User input error checking using text file in Java -


i have text file room numbers in school. have code asks room number of student's class. kb user input scanner , roomnos text file scanner. code:

string room = null; boolean invalid = true; while(invalid) {     system.out.println("what room number of class?");     room = kb.nextline();     while(roomnos.hasnext())     {         string roomno = roomnos.nextline();         if(room.equals(roomno))             invalid = false;     }     if(invalid == true)     {         system.out.println("the room number you've given invalid.");     } } 

the code works if first room number given user valid. however, if user inputs invalid room number, loop not stop, is, says room number invalid , asks room number again , again, if room number valid. think hasnext() in nested while loop not restart , go beginning of file , hence, cannot search room number during next loops of bigger while loop. whatever problem is, how can fix it?

your check invalid input happens outside of while-loop and loop has no breaking condition @ all. move check inside of loop , add breaking condition leave loop @ time.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -