How to use while loop for text file?
-
I am having problem with the while loop for the text file. Can anyone help me please? Much help is appreciated. Thanks. Part A Instead of writing a separate line to read each word (or number / item) in your text file use a while loop and have the ending condition be the false for the next statement. Part B Generate 10 random numbers between 100 and 200 and write them to a text file. I get stuck at the while loop part for Part A. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Project7 { public static void main(String[] args) throws FileNotFoundException { File file = new File ("word.txt"); Scanner myScan = new Scanner (file); String fil = myScan.next(); System.out.println(fil); System.out.println(myScan.next()); System.out.println(myScan.next()); System.out.println(myScan.hasNext()); } }
-
Answer:
I think you need something like String fil; while ( (file = myScan.next())!= null){ System.out.println(fil); } or while(myScan.hasNext()){ System.out.println( myScan.next()); } hope it helps!
Sayee at Yahoo! Answers Visit the source
Related Q & A:
- How to delete row from text file?Best solution by Stack Overflow
- How to extract information from text file in Python?Best solution by Stack Overflow
- How to Creating text File using Python?Best solution by Stack Overflow
- How to use BitmapFont.TextBounds to center a line of the text on the screen?Best solution by Stack Overflow
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.