-2

Just wondering if you could help with this issue. code and output provided below.

do {
    System.out.println("would you kindly enter your age?");
    while (!sc.hasNext("[0-9]+")) {
        System.out.println("Please only use numbers.");
        sc.nextLine();
    }
    age = sc.nextByte();
    if (age >= 18) {
        System.out.println(""+age);}
    else if (age <= 17) {
        System.out.println("You must be 18 or older to rent a car if you input an incorrect age please try again otherwise close this page.");
    }
} while (age <= 17);

System.out.println("Would you kindly enter the first line of your address?");
address1 = sc.nextLine();

System.out.println("Would you kindly enter the second line of your address?");
address2 = sc.nextLine();

System.out.println("Would you kindly enter the third line of your address");
address3 = sc.nextLine();

sc.close();

Output:

Would you kindly enter your name?
m
would you kindly enter your age?
122
Would you kindly enter the first line of your address?
Would you kindly enter the second line of your address?
1
Would you kindly enter the third line of your address
1

Tom
  • 14,120
  • 16
  • 41
  • 47
mark c
  • 1
  • 1
  • Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – Timothy Truckle Jan 08 '17 at 22:06

3 Answers3

1

That is because the Scanner.nextInt method does not capture the last newline character of your input (which is after number, e.g. '5\n'), and that newline is loading into String while calling Scanner.nextLine();

anodyna
  • 41
  • 3
0

changing nextline to just next under each address seemed to resolve the matter, don't know why but it works.

also thank you for the help tom.

mark c
  • 1
  • 1
0

Try something like this:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Continue? [Y/N]:");
    while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {
      System.out.print("Would you kindly enter your age?");
      int age = sc.nextInt();
      sc.nextLine(); // To consume the left over newline;
      String address1, address2, address3;
      if (age >= 18) {
        System.out.println("Since you are " + age + " you are old enough to rent a car");
        System.out.println("Would you kindly enter the first line of your address?");
        address1 = sc.nextLine();
        System.out.println("Would you kindly enter the second line of your address?");
        address2 = sc.nextLine();
        System.out.println("Would you kindly enter the third line of your address");
        address3 = sc.nextLine();
      } else if (age <= 17) {
        System.out.println("You must be 18 or older to rent a car if you input an incorrect age please try again otherwise close this page.");
      }
      System.out.print("Continue? [Y/N]:");
    }
  }
}
Sash Sinha
  • 11,515
  • 3
  • 18
  • 35