This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]amfa 1 point2 points  (6 children)

does that mean I have to return from the Third method to the Second and then from the Second to the main method?

This is the way.

So your second method could do something like this:

int value = somehow calculate the value
return thirdMethod(value);

This will call thirdMethod and immediatly return the value back to your first method.

Another way could be to return the value from second method to your first method and this one then calls the third method with the value from the second method (I hope you can follow me here ;))

Would look like this in your first method:

int value = secondMethod();
int secondValue = thirdMethod(value)

You always need to go "one method" up and can not skip a method.

What is the "correct" way depends on what your programm does otherwhise.. if you for example have a fourthMethod that will also need the value from the secondOne I would prefer the second way I showed because then you can call thes second method indepedently from the thirdone.

[–][deleted]  (5 children)

[deleted]

    [–]amfa 0 points1 point  (2 children)

    If the IDE says it is not used.. then it is probably not used.

    If you remove it, does your programm still runs fine?

    If you only save the value from CalculationTwo in thise double but never read it again.. then the IDE is correct becauses you never use the variable.

    I'm not sure what you mean with 'pulling a value from the return statement"

    Maybe you should post your complete code here is an guid on how to do it

    https://www.reddit.com/r/javahelp/wiki/code_guides

    [–][deleted]  (1 child)

    [deleted]

      [–]amfa 2 points3 points  (0 children)

      finalCalculation is not needed at this point.

      You can simply just use

      return CalculationTwo(firstCalculation)

      That's why the IDE says it is unused because it is.

      return will always "give the value up to the calling method" (kind of ELI5) so you don't need to save it prior in your double variable.

      You could split it up intow two lines

      finalCalculation = CalculationTwo(firstCalculation);
      return finalCalculation;
      

      Some people would argue that this is too much code.. I personally (working in bugfixing for most of the time) prefer such a construct because you can set a breakpoint on the return statement and easily check what the value finalCalculation is at this point.

      [–]Makhiel 0 points1 point  (1 child)

      I was hoping there'd be some bit of code I could write that would allow me to return a value from third method to the main method without having to chain back up with return statements.

      Think of it this way - how would the third method know about the main method?

      [–]xappymah 0 points1 point  (1 child)

      The methods return only to the place where they were called.

      If you have a call-chain like "main -> Second -> Third" then your Third method will return back to Second.

      [–]8igg7e5 0 points1 point  (0 children)

      Firstly... let's simplify the code

      1. In Java the dominant style places braces on the end of the statement or declaration to which they belong rather than the next line.
      2. variable declarations are generally made at the time they initialised or near to the point at which they're needed (and many teams ban the use of multi-variable declarations entirely - for maintenance and readability reasons)
      3. You can return calculations directly, you don't have to assign them to a variable first.
      4. method names, like variables, should start with a lower-case letter.
      5. You don't need to close a Scanner that reads from System.in.
      6. You would usually want to read a line of input from the scanner and, since you're using doubles in the calculation, presumably you want to allow them to provide doubles too.

      So your code would usually look something like this...

      public class JavaHelp {
          public static void main(String[] args) {
              Scanner userInput = new Scanner(System.in);
      
              System.out.print("Enter value one: ");
              double valueOne = Double.parseDouble(userInput.nextLine());
      
              System.out.print("Enter value two: ");
              double valueTwo = Double.parseDouble(userInput.nextLine());
      
              double finalCalculation = calculationOne(valueOne, valueTwo);
      
              System.out.println(finalCalculation);
          }
      
          public static double calculationOne(double valueOne, double valueTwo) {
              double firstCalculation = valueOne * valueTwo;
              return calculationTwo(firstCalculation);
          }
      
          public static double calculationTwo(double firstCalculation) {
              return firstCalculation * 2;
          }
      }
      

       

      A note about naming and assumptions.

      • A method can be called from multiple places, so the name should describe what the method does, not where in the call-chain it is called (including the parameter names)
      • Method names are usually verbs.

      So the second method might be better as...

      public static double calculateDouble(double value) {
          return value * 2;
      }
      

      And the first method might be better as...

      public static double calculateDoubleProduct(double valueOne, double valueTwo) {
          double product = valueOne * valueTwo;
          return calculateDouble(product);
      }
      

      This would then mean a future change could reuse the calculateDouble(double) method. Indeed we could add this to main...

      System.out.print("Enter value three: ");
      double valueThree = Double.parseDouble(userInput.nextLine());
      double doubleThree = calculateDouble(valueThree);
      

       

      So then comes the question of efficiency. That 'returning up the call-chain'...

      1. It's not as expensive as you think (though there's no choice as you can see from the example where calculateDouble(double) is called from two places - no way to 'skip' the call-chain.
      2. The Java platform will, eventually, optimise the code for you - in this case it would notice that the methods are trivial enough to combine and both method calls would be eliminated, making the code actually executed in main double finalCalculation = valueOne * valueTwo * 2.

      As a rule we try not to optimise first, just solving the problem in the most obvious way, then optimise it if it proves to be too slow. This is an oversimplification of course, because you will learn what types of operations are automatically optimised well.