| | |
infinite loop with user input
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 10
Reputation:
Solved Threads: 0
hi guys, I am having trouble with this code, cause i get an infinite loop. I made nexTxtFile the variable name of the txt file i am creating and writing text to. i want to be able to get user input untill the user does not want to enter anymore text but i don't know how to do that. i used while (scan.hasNext()) but that just loops to infinity. Without the while loop i just get only the first word i type in as user input, how can i fix this? may i please have an example?
thanks ippoMarley
File myFile = new File(newTxtFile);
Writer output = new BufferedWriter(new FileWriter(myFile));
while (scan.hasNext()) {
txt = scan.next ();
output.write(txt);
}
output.close();thanks ippoMarley
The
These are the lines from the Scanner docs:
What you could do is ask the user to input a specific pattern like 'exit' or 'quit' when he's done giving the input and then scan the input looking for this token.
hasNext() method blocks waiting for user input, so the scanner always has a token to show up in hasNext and the while loop never actually terminates.These are the lines from the Scanner docs:
The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block. What you could do is ask the user to input a specific pattern like 'exit' or 'quit' when he's done giving the input and then scan the input looking for this token.
Last edited by verruckt24; Apr 5th, 2009 at 4:08 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
![]() |
Similar Threads
- Infinite Loop (C++)
- Problem with loop, infinite or no loop at all (Java)
- program goes in infinite loop?? (C++)
- Handling Invalid User Input? (C)
- Type Conversion of User Input: (C++)
- infinite loop... (C++)
Other Threads in the Java Forum
- Previous Thread: Subclass help
- Next Thread: why showing only one ???
Views: 795 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database detection draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linux list loop map method methods mobile netbeans newbie number object objects oracle oriented os panel print problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows





