0

I'm having problem with outputting two separate questions. The code is:

System.out.println("Please enter full name: ");

String name = keyboard.nextLine();

while (name.length() >= 21) 
{
    System.out.println("Name is invalid, please re enter:");
    name = keyboard.nextLine();
}


System.out.println("Please enter reference number: ");
String reference = keyboard.nextLine();

while (reference.length() > 6) {
    System.out.println("Refrence incorrect, please re enter");
    reference = keyboard.nextLine();
}
while (!reference.matches("(?i)[A-Z]{2}[0-9]{3}[A-Z]")) {
    System.out.println("Reference is incorrect, please re-enter:");
    reference = keyboard.nextLine();

however what is being output resembles:

Please enter full name:

Please enter reference number: 

with no room for me to input the name or reference. Even when i do, it asks for the reference again. Can anybody spot any problems in my code? (I'm a beginner if you couldn't tell my the un elegant coding haha)

the code that came before this is:

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please select from the following options:");
    System.out.println("1. Enter new Policy");
    System.out.println("2. Display summary of policies");
    System.out.println("3. Display summary of policies for selected month");
    System.out.println("4. Find and display Policy");
    System.out.println("0. Exit");
    int option = keyboard.nextInt();

    if (option == 1) {
        System.out.println("Please enter full name: ");

...

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
justBecca
  • 133
  • 2
  • 13

1 Answers1

1

The problem is that when you call

int option = keyboard.nextInt();

It doesn't read the last newline character, you could solve this by calling

int option = Integer.parseInt(keyboard.nextLine());

nextLine() will also consume the newline character, but it will return a String, so you need to parse it as an Integer.

Edit:

If the input (in nextInter) is not an Integer, you will get an NumberFormatException. To handle this you must use a try-catch clause:

int option;
try {
    option = Integer.parseInt(keyboard.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
}
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • when i put the second part in about the try catch clause, then the: if (option == 1) doesn't work? Do you know how to fix this? – justBecca Jan 12 '14 at 21:51
  • Why it doesn't work? Do you get an error? Try printing `option` so you know what value is. – Christian Tapia Jan 12 '14 at 22:22
  • Sorry my fault, it must be `keyboard.nextLine()`, not `input.nextLine()`. Edited. – Christian Tapia Jan 12 '14 at 22:24
  • no, it just doesnt recognise that they have the same value. I fixed it by declaring option at the start of the code, but not sure if that was the best possible way? – justBecca Jan 12 '14 at 22:46
  • It's an scope issue, since `option` was declared inside the `try-block` you couldn't use it after the block. Try reading about **scopes** in Java. – Christian Tapia Jan 12 '14 at 22:58