0
import java.util.Scanner;

public class Vending {
    public double vend(double balance) {
        Scanner in = new Scanner(System.in);
        balance = 0;
        System.out.print("Enter a command = ");
        String command = in.nextLine();

        while (in.hasNext()) {
            if (command.equals("penny")) {
                balance = balance + 0.01;
                System.out.println("balance = " + balance);
            }
            return balance;
        }
    }
}

Hi! I have tried everything to figure out why the return statement is not being recognized. If I put the "return balance" anywhere else it says that the system.out.println is unreachable... Can any of you kindly help me out as to why this may not be working?? Thank you!!

6
  • 1
    Your code would be much easier to understand - both for you and us - if you would indent it. If you're using an IDE like Eclipse, you can probably ask it to reformat your source code for you. Once you've done that, the answer may be much more obvious. (I suspect I know what's wrong already, but I want you to reformat the code and see if you can find it yourself.) Commented Mar 15, 2013 at 20:15
  • Ah, except I see it's been done already :( Commented Mar 15, 2013 at 20:15
  • 2
    I thought it was faster to indent it myself than to ask the OP to do it :) Commented Mar 15, 2013 at 20:16
  • It seems like there is a logic issue. You have to re-read the new line every loop as opposed to reading it once and expecting the loop to work as intended. Commented Mar 15, 2013 at 20:16
  • That said, accidentally I removed the actual question... Fixed now, sorry for that. Commented Mar 15, 2013 at 20:18

4 Answers 4

3

The return statement is inside the while loop and outside the if condition segment, so the code its breaking the loop on the first iteration.

Possible solution: move the return outside the while

while (in.hasNext()) {
    if (command.equals("penny")) {
        balance = balance + 0.01;
        System.out.println("balance = " + balance);
    }
}
return balance;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi! I've tried moving the return outside the while but then I get "Exception in thread "main" java.lang.NoSuchMethodError:main" , when I left all else constant. Do you know what may be the problem then?
@BettyJones well, you fixed the vend method problem. Now, make sure you have declared the main method as public static void main(String[] args). If you have a new problem, it would be better to post a new question.
1

Your method vend(double balance) is defined to return a double. Whatever happens within the method, it must return a double. Now you have a return statement here:

while (in.hasNext()) {
    if (command.equals("penny")) {
        balance = balance + 0.01;
        System.out.println("balance = " + balance);
    }
    return balance;
}

But what happens if your statement in.hasNext() returns false? Then the return inside it will never be reached. Therefore the compiler can not guarantee that your method is valid java and is therefore complaining.

You should add a return statement outside the while.

When you say

If I put the "return balance" anywhere else it says that the system.out.println is unreachable

You put the return statement right before a System.out.println() statement. When a method returns, it gives up on anything that might've happened after the return statement, basically making anything after it useless. The return statement must be the last statement in some execution branch of your method.

Comments

0

That's because the compiler can't guarantee that the return statement will execute because it is within your loop. It has to assume that in.hasNext() may return false and never hit that return.

Comments

0

You're getting the "Missing return statement" error because there is a possiblity that your while loop is never executed. This leads to the program reaching the end of the function without ever returning.

while (in.hasNext()) {
  if (command.equals("penny")) {
    balance = balance + 0.01;
    System.out.println("balance = " + balance);
  }
  return balance;
}

If we pretend in never has any lines then the program passes the while and therefore the return causing this issue. For your case you can probably just have a default return such as return 0; or however you'd like to handle the situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.