0

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);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • After `nextInt` you have to call `nextLine` to consume the dangling "enter" key press – QBrute Jan 15 '22 at 15:34
  • 1
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Silvio Mayolo Jan 15 '22 at 15:37
  • The problem is more likely that you call `sc1.close();` which closes `System.in`, making reading further input from `System.in` impossible. – Thomas Kläger Jan 15 '22 at 15:53
  • @ThomasKläger, for begin im trying this construction whith one Scanner (iniciate it before loops and close after all loops), but got the same result. – IvMovchanets Jan 15 '22 at 15:58
  • @SilvioMayolo nope( i try few combinations - but didnt helps( – IvMovchanets Jan 15 '22 at 16:00
  • This is the next problem: if `sc1.hasNextInt()` returns false the scanner has read something that is not an int. To make progress you need to skip this something in your `else` clauses by calling `sc1.next();` (before or after writing the error message). – Thomas Kläger Jan 15 '22 at 16:09
  • @Thomas Kläger, thank you! 2 days thinking about this problem, already everything works. Your mission for today ended, one happy man in Lithuania) good luck. – IvMovchanets Jan 15 '22 at 20:08

1 Answers1

0

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);