0

This is probably a really simple mistake, but I'm a beginner with Java (and programming in general) and I was curious why the break statement won't work in my program.

public class ValidateStudentNumber
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        do{
        System.out.println("Enter a number: ");
        String number = kb.next();

        boolean valid = true;

        for (int i = 0; i < number.length(); i++){
            char c = number.charAt(i);
            if(! Character.isDigit(c)) {
                valid = false;
                break;
            }
            if ( number.length() != 7) {
                valid = false;
            }
        }
        if (valid){
            System.out.println("Valid");
        }
        else{
            System.out.println("Invalid");
        }
    }
    while (kb.hasNext());
    System.out.println ("Program ending due to end-of-file");
    }
}

What I want to accomplish with this code is that as soon as a letter is typed, the breaks out of the for statement and displays "Invalid", yet when I run the program and type something that is a non-digit, it doesn't leave the for loop and allows me to continue typing as usual.

Thank you for your time.

  • 4
    why do you need to continually check `number.length` inside the loop? if the input isn't the correct length, checking the length everytime for the length INSIDE the loop is pointless/repetitive. if it's not the correct length, there's no point in checking anything else - it's outright wrong, even if there's only digits. – Marc B Oct 26 '16 at 19:48

4 Answers4

1

Break statement is working fine. Because it breaks the execution of for loop not for do-while loop. If you want to break do-while loop add break statement in not valid case else block.

public class A
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);
        do{
        System.out.println("Enter a number: ");
        String number = kb.next();

        boolean valid = true;

        for (int i = 0; i < number.length(); i++){
            char c = number.charAt(i);
            if(! Character.isDigit(c)) {
                valid = false;
                break;
            }
            if ( number.length() != 7) {
                valid = false;
            }
        }
        if (valid){
            System.out.println("Valid");
        }
        else{
            System.out.println("Invalid");
            break;
        }
    }
    while (kb.hasNext());
    System.out.println ("Program ending due to end-of-file");
    }
}
Deepak Agrawal
  • 1,107
  • 3
  • 17
  • 41
1

What I want to accomplish with this code is that as soon as a letter is typed, the breaks out of the for statement and displays "Invalid"

If I understand your question correctly, you want your loop to break when you literally hit a non-digit key on the keyboard while entering a number.

From the Scanner java docs...

A scanning operation may block waiting for input.

What is probably happening is the console is blocked waiting for a carriage return (i.e. for you to hit Enter) to then process your input. So your program is not running for every key press, but is waiting for an entire line to be entered.

This is most apparent when you remove your do-while loop. When that is removed, you can see you can enter many characters until you hit Enter where your program determines if the string entered is valid then the program ends.

In summary, program not run for every key press, just when you hit "Enter".

Edit -

Here's a question (and possible solution to your problem) trying to figure out how to read one character at a time. Looks like you need to put your console in "raw" mode.

How to read a single char from the console in Java (as the user types it)?

Community
  • 1
  • 1
FriedSaucePots
  • 1,282
  • 8
  • 15
0

break will make you exit the loop containing the break statement i.e. in your code it will break you out of the for-loop. Then it keeps on waiting for input from scanner. As soon as you type something again, condition is true and then the loops re-runs.

Nadim Baraky
  • 461
  • 5
  • 18
Rusheel Jain
  • 778
  • 5
  • 17
0

You seem like you wanted to break out of the do/while not just the for loop. This can be accomplished by labeling your loops. So you could rewrite your code like:

class ValidateStudentNumber
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        outer:do{
           System.out.println("Enter a number: ");
           String number = kb.next();

           for (int i = 0; i < number.length(); i++){
              char c = number.charAt(i);
              if(! Character.isDigit(c) || number.length() != 7) {                    

                  System.out.println("Invalid");
                  break outer;
              }
           }//end of for loop  
    }while (kb.hasNext());
    System.out.println ("Program ending due to end-of-file");
    }
}
Jay Laughlin
  • 134
  • 1
  • 6