-5
public static void add2q() {
    System.out.print("Age:");
    age = sc.nextInt();
    if (!(age >= 3 && age <= 80)) {
        System.out.println("Sorry you must be between the age of 3 to 80");
    } else if (age >= 3 && age <= 5) {
        while (true) {
            System.out.println("Need to be accompanied by an adult above 16\nHave an adult to accompany?y/n");
            String yn = sc.nextLine();
            if (yn.equals("y")) {
                System.out.print("Child's Name: ");
                name = sc.nextLine();
                names.add(name);
                price();
                addAdult();
                break;
            } else if (yn.equals("n")) {
                while (true) {
                    System.out.println("Sorry you cannot take this ride");
                    add2q();
                    break;
                }
            } else {
                System.out.println("Invalid output"); //funny problem here 
            }
        }
    } else {
        System.out.print("Name: ");
        name = sc.nextLine();
        names.add(name);
        price();
    }
}

When user types in 3 or 4 or 5, it will print the "need to be..." as well as the "Invalid output statement", but I don't want the last statement. How can I prevent the last statement from being shown?

gobernador
  • 5,271
  • 3
  • 28
  • 50
Rae-Win
  • 3
  • 5
  • What do you type after "3 or 4 or 5"? – cs95 Jun 20 '17 at 13:27
  • 1
    Well that what you code is doing. If the age is in the range (3:5) but the next input (`yn`) is not `"y"` or `"n"` it will output an invalid output. Just update your condition if you don't want that – AxelH Jun 20 '17 at 13:30
  • Suppose to type y/n but before i type that it will print "Invalid input" @Coldspeed – Rae-Win Jun 20 '17 at 13:30
  • @AxelH yeah before i could type the y/n it will print "Invalid input" that's the problem – Rae-Win Jun 20 '17 at 13:31
  • Then read the duplicate ... this is a common error. But this would have been easy to find with google. – AxelH Jun 20 '17 at 13:31

2 Answers2

0

If you use

age = sc.nextInt();

and after use

String yn = sc.nextLine();

yn it´s the final of the line ( it's = "") it's better use age = Integer.parseInt(sc.nextLine()) if your input is per line.

An example:

if your input is

3 y

in this case the program prints

"Need to be accompanied by an adult above 16\nHave an adult to accompany?y/n"

but also prints

"Invalid output"

this because yn has the value "enter"(like said Alex) and the value "y" is captured in the second iteration.

Fabian
  • 73
  • 8
  • To explain a bit more. `nextInt` don't consume the "enter" key of the scanner, so every `nextLine` that follow will notice it and will read that. And the usage of `Integer.parseInt` is better to incorrect input, not better to parse the content – AxelH Jun 20 '17 at 13:33
  • That´s right, it´s not the best way to parse the content. – Fabian Jun 20 '17 at 13:45
0
sc.nextInt()

does not consume the new line character in your input. So when you type 3 and hit enter your input will be:"3\n" but the scanner will only read in the 3. Therefore when you call

sc.nextLine()

The scanner will read in that remaining newline character which messes up your input. You can work around this by calling the nextLine method right after your scanner.nextInt() method to consume the remaining newline character like so

sc.nextInt();
sc.nextLine();

Or you can follow Fabian's suggestion and use Integer.ParseInt() along with sc.nextLine()

Malphrush
  • 326
  • 3
  • 12