Search code examples
javaloopsif-statementjava.util.scannerinfinite-loop

Scanner method .hasNextInt() and if statement - works only 1 time. For next loop - automatically (didn't wait any input) gives false


I tried to do an input check (need to take 3 numbers using Scanner). Before that, I used a similar method (.hasNext(int)) in another task - everything worked fine. In this case, it doesn't work. The first "while" loop works correctly, on the second loop .hasNextInt() returns false and loops the loop - not giving the opportunity to enter data.

boolean num1IsInt = false;
boolean num2IsInt = false;
boolean num3IsInt = false;
int scaicius1 = 0;
int scaicius2 = 0;
int scaicius3 = 0;

System.out.println("Įveskite 3 skaičiai, po viena after each press enter");

while (!num1IsInt) { //check first number is int
    Scanner sc1 = new Scanner(System.in);
    System.out.println("(1)Įveskyte pirmas skaicius");
    if (sc1.hasNextInt()) {
        scaicius1 = sc1.nextInt();
        num1IsInt = true;
    } else {
        System.out.println("Not correct integer");
        continue;
    }
    sc1.close();
}

while (!num2IsInt) { //check second number is int
    Scanner sc2 = new Scanner(System.in);
    System.out.println("(2)Įveskyte antras skaicius");
    if (sc2.hasNextInt()) {
        scaicius2 = sc2.nextInt();
        num2IsInt = true;
    } else {
        System.out.println("Not correct integer");
        continue;
    }
    sc2.close();
}

while (!num3IsInt) { //check third number is int
    Scanner sc3 = new Scanner(System.in);
    System.out.println("(3)Įveskyte trecias skaicius");
    if (sc3.hasNextInt()) {
        scaicius3 = sc3.nextInt();
        num3IsInt = true;
        sc3.close();
    } else {
        System.out.println("Not correct integer");

        continue;
    }
    sc3.close();
}

System.out.println("First number = " + scaicius1);
System.out.println("First number = " + scaicius2);
System.out.println("First number = " + scaicius3);

Solution

  • Thank you - @Thomas Kläger. I change code like was at the begin (with only one Scanner, cose with 3 scanners it's didn't work) and add to all else statements reader for this "ghost element which loop my code".

            boolean num1IsInt = false;
            boolean num2IsInt = false;
            boolean num3IsInt = false;
            int scaicius1 = 0;
            int scaicius2 = 0;
            int scaicius3 = 0;
    
            System.out.println("Įveskite 3 skaičiai, po viena after each press enter");
            **Scanner sc = new Scanner(System.in);**
    
            while (!num1IsInt) { //check first number is int
                System.out.println("(1)Įveskyte pirmas skaicius");
                if (sc.hasNextInt()) {
                    scaicius1 = sc.nextInt();
                    num1IsInt = true;
                } else {
                    System.out.println("Not correct integer");
                    **sc.next();**
                }
            }
    
            while (!num2IsInt) { //check second number is int
                System.out.println("(2)Įveskyte antras skaicius");
                if (sc.hasNextInt()) {
                    scaicius2 = sc.nextInt();
                    num2IsInt = true;
                } else {
                    System.out.println("Not correct integer");
                    sc.next();
                }
            }
    
            while (!num3IsInt) { //check third number is int
                System.out.println("(3)Įveskyte trecias skaicius");
                if (sc.hasNextInt()) {
                    scaicius3 = sc.nextInt();
                    num3IsInt = true;
                } else {
                    System.out.println("Not correct integer");
                    sc.next();
                }
            }
            sc.close();
    
            System.out.println("First number = " + scaicius1);
            System.out.println("First number = " + scaicius2);
            System.out.println("First number = " + scaicius3);