-4

I'm trying to delete the user integer input from an array. However, I can't get the value of the input.

while(keepGoing)
    {
        while (!scan.hasNextInt() ) 
        {        
            scan.next(); 
            System.out.print('\n'+"Choose a valid number: "); 
        }
        int unitA = scan.nextInt();
        if (unitA < 1) 
        {
            System.out.print('\n'+"Choose one of the options: ");
            keepGoing = true;
        }
        else if (unitA > 14) 
        {
            System.out.print('\n'+"Choose one of the options: ");
            keepGoing = true;
        }
        else
            lengthValue.remove(unitA);
        scan.close();
        keepGoing = false;
    }
    //lengthValue.remove(int unitA);
    System.out.println(unitA);
arnt
  • 8,949
  • 5
  • 24
  • 32
Sqad0ra
  • 3
  • 1
  • Well, `hasNextInt()` and `next()` look for totally different tokens. Why are you testing the stream this way? – markspace Jan 17 '19 at 00:35
  • Im just trying to avoid the string inputs.. i know its a strange way , do u suggest any better way for that ? – Sqad0ra Jan 17 '19 at 00:38
  • I reindented your code for you so it matches the braces. Look closely. – arnt Jan 17 '19 at 07:51

1 Answers1

0

In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.

You can find a similar question here: How to use Scanner to accept only valid int as input

kader
  • 24
  • 1
  • 4
  • `System.out.println(unitA);` cant define **unitA** – Sqad0ra Jan 17 '19 at 00:54
  • Since unitA is defined inside your while loop, you cannot reach it outside the loop, it becomes out of the scope. What you need to do is to define unitA just before the "while" statement. And assign its values inside the loop. – kader Jan 17 '19 at 00:58