-1

When I run my code it works fine until it gets to the point where my if statement evaluates the answer string. It will always run the first if statement no matter what I input into the scanner object. And if I take out the scanner.nextLine(); then it will not let me enter any input for the answer object.

public class ExceptionTest {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean statement = true;
    int total; 
    String answer = null;
    do{
            try{
                System.out.println("Please enter the amount of Trees:");
                int trees = scanner.nextInt();
                 if(trees < 0){
                    throw new InvalidNumberException();
                }
                System.out.printf("Amount of fruit produced is: %d%n", trees * 10);
                System.out.println("Please enter the amount of people to feed: ");
                int people = scanner.nextInt();
                scanner.nextLine();
                total = trees * 10 - people * 2;
                System.out.printf("The amount of fruit left over is: %d%n", total);
                statement = false;
            }
           catch(InvalidNumberException e){
                System.out.println(e.getMessage());
                scanner.nextLine();}
        }
    while(statement);
        System.out.println("Would you like to donate the rest of the fruit? Y or N:");
        try{
            answer = scanner.nextLine();
                
                
                
            if(answer == "Y"){
                System.out.println("Your a good person.");
            }else if(answer == "N"){
                System.out.println("Have a nice day.");
            }else {
                throw new NumberFormatException();
            }
        }
        catch(NumberFormatException e){
            System.out.println(e.getMessage());
            scanner.nextLine();
    }

  }
}
 
Anubis
  • 5
  • 3

2 Answers2

0

There are three things here:

  1. The first call to scanner.nextLine() gets the user input, but isn't stored in a variable. The answer variable is storing a second, unnecessary, call to scanner.nextLine(). EDIT The reason why scanner.nextLine() is needed is the previous scanner call is to scanner.nextInt(). nextInt() doesn't move the cursor to the next line. See: Scanner is skipping nextLine() after using next() or nextFoo()?.

  2. You can't compare Objects using == and !=. Java's comparison operators test for strict object equality. You don't mean to check for whether they are the same String instance, you mean to check if the two instances store the same text. The correct comparison is !answer.equals("Y").

  3. Think about the logic of the if statement. Think about what the || operator means.

a p
  • 710
  • 1
  • 5
  • 21
  • Without the extra scanner.nextLine() it skips to the if statement. Not allowing my to type any input for the answer variable – Anubis Apr 22 '21 at 01:04
  • @Anubis I'm unable to replicate that behavior with the code you posted. Can you edit the question to include the entire class? – a p Apr 22 '21 at 01:06
  • @Anubis I've edited my answer. You need to be wary of using 'nextInt()` in a loop. It doesn't consume the new line, so the scanner was expecting the user input to be on the same line. Check out the answer I linked. – a p Apr 22 '21 at 01:19
  • With the edit I was able to get my code up and working. Thank you. – Anubis Apr 22 '21 at 02:22
0

I have seen that you edited your answer which was good because the logic was wrong in there. You should check first if the input was Y or N before verifying it is not in the two. Your problem is pretty simple, it is a common problem regarding the scanner class. Just add String trash = scanner.nextLine(); and remove many unnecessary scanner.nextLine();

        String trash = scanner.nextLine();
        System.out.println("Would you like to donate the rest of the fruit? Y or N:");
        try {
            answer = scanner.nextLine();

            if (answer.equals("Y")) {
                System.out.println("Your a good person.");
            } else if (answer.equals("N")) {
                System.out.println("Have a nice day.");
            } else {
                throw new NumberFormatException();
            }
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
        }

Add String trash whenever the Scanner messes up in your case it was near the answer=scanner.nextLine(); If there are still errors just comment. I am happy to help

Gerome Tahud
  • 100
  • 8