Results 1 to 6 of 6
- 10-30-2010, 04:43 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
I feel dumb asking this... File reader loop
Hello everyone! Happy Halloween Weekend!
I am trying to read 10 lines in a text file. I can read the first one, but it wont proceed past the first line! I have tried to put the command to keep going all over the place -- it never works besides reading the first line. Am I missing something too obvious here?
Java Code:import java.io.IOException; import java.util.*; public class PhoneError { public static void main(String[] args) throws Exception { int area = 0; int validArea = 0; int counter = 0; java.io.File file = new java.io.File("phone_numbers.txt"); Scanner scan = new Scanner(file); /**** this gets the first line, as it should ****/ String number = scan.nextLine(); if( !(number.length() == 10)) { System.out.println("not valid phone number"); } else { area = Integer.parseInt(number.substring(0, 3)); validArea =+ 1; if( validArea >=1 ) { System.out.println("valid area code is " + area); counter=+1; System.out.println(counter); /****I have put the below line all over this code - nothing works to have it read the next line! *******/ number = scan.nextLine(); } } } }
- 10-30-2010, 05:18 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:/****I have put the below line all over this code - nothing works to have it read the next line! *******/ number = scan.nextLine();
Depending on what you want to do the line shuld probably go after the if-else blcoks have finished.
You can use System.out.println("number="+number) after each of the nextLine() calls to see what you have actually read. Check this with the contents of the file.
- 10-30-2010, 05:45 AM #3
You need to create a loop of some sort that goes until the file has no more lines.
Sincerely, Joshua Green
Please REP if I help :)
- 10-30-2010, 08:18 AM #4
Member
- Join Date
- Oct 2010
- Location
- Fort Wayne, IN
- Posts
- 7
- Rep Power
- 0
Use a loop to lop through the file
Here is what you need:
Put your codes inside a while loop so that your program will keep executing as long as the file has more data to read.
while(scan.hasNext())
{
put your codes here
}Last edited by Honopac; 10-30-2010 at 09:26 AM.
- 10-30-2010, 10:41 AM #5
- 10-30-2010, 01:18 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
problem with file reader
By Stormrage in forum New To JavaReplies: 7Last Post: 05-15-2010, 11:48 PM -
Selecting an input file for Reader
By myazuid in forum New To JavaReplies: 3Last Post: 11-25-2008, 01:21 AM -
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks