1

So, this program works, it runs, but I want it to be idiot proof! And to do that I need some help.. I don´t want it to go to the next line if the user entered a number or nothing at all!

If I press enter without writing anything at all, it still goes to the next line, and the variable navn comes out completely empty at the end. It does the same thing if I write a number. How do I make it go back and try the same while loop again if for the answer doesn't fulfil the prompts. Thank you very much:)

import java.util.Scanner;

class Metoder {

    public static void main(String[] args) {
        String bosted; //Variable
        String navn; //Variable

        Scanner in = new Scanner(System.in);

        System.out.println("Skriv inn navn: "); //What shows up when you first start the program

        while (!in.hasNext("[A-Za-z]+")) { //Only allow letters A-Z
            in.next();
            System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number

        }
        System.out.println("Takk!"); //Says thank you if the user has entered letters

        navn = in.nextLine(); //Proceeds to next line

        System.out.println("Skriv inn bosted: "); //Next line, where the user is supposed to enter where he/she lives
        while (!in.hasNext("[A-Za-z]+")) { //Excactly the same loop as above
            in.next();
            System.out.println("Tall hører ikke hjemme i stedsnavn, prøv igjen!");
        }
        System.out.println("Takk!");

        bosted = in.nextLine();

        System.out.println("Hei, " + navn + "! Du er fra " + bosted + "."); //Prints out what the user has entered previously in a full sentence.

    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Bjørn Olav Jalborg
  • 373
  • 1
  • 3
  • 14
  • 1
    When you use several times the same piece of code, consider putting it inside a dedicated method. – Arnaud Aug 30 '16 at 14:00
  • put everything inside a while loop exit the while loop only when the variable contains any value – Anoop LL Aug 30 '16 at 14:02

2 Answers2

1

Please use this code :

  public static void main(String[] args) {
    String bosted=""; //Variable
    String navn=""; //Variable

    Scanner in = new Scanner(System.in);

    System.out.println("Skriv inn navn: "); //What shows up when you first start the program

    while(in.hasNext()) { //Only allow letters A-Z
         navn = in.nextLine();
        if(!navn.isEmpty() && navn.matches("[A-Za-z]+")){
             System.out.println("Takk!"); //Says thank you if the user has entered letters
             in.next();
             break;
        }
        else{
            System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number
            in.next();
            System.out.println("Skriv inn navn: ");
            continue;
        }

  }

    System.out.println("Skriv inn bosted: ");
    while(in.hasNext()) { //Only allow letters A-Z
        bosted = in.nextLine();
       if(!bosted.isEmpty() && bosted.matches("[A-Za-z]+")){
         System.out.println("Takk!"); //Says thank you if the user has entered letters
         break;
       }
       else{
           System.out.println("Tall horer ikke hjemme i navn, prøv igjen!"); //Prints, "numbers dont belong in names, try again" if what the user entered is a number
           in.next();
           System.out.println("Skriv inn bosted: ");
           continue;
       }


 }
    System.out.println("Hei, " + navn + "! Du er fra " + bosted + "."); //Prints out what the user has entered previously in a full sentence.
}  
0

You can use the continue keyword to restart a loop essentially.

while(!in.hasNext("[A-Za-z]+")) {
    try {
        String s = in.nextLine();
        if(s.isEmpty() || s.matches("^-?\\d+$")){
            throw new Exception("empty string or number detected");
        }
    } catch (Exception e){
        continue;
    }
}

In that if condition we are checking if the entered string is empty or is an integer, and we can throw an exception which will cause the while loop to continue asking for input until the condition fails (i.e. passes our test).

Community
  • 1
  • 1
px06
  • 2,256
  • 1
  • 27
  • 47