Results 1 to 6 of 6
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default 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();
    	   }
    		  
    	}
    	    
     }
    		
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default

    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.

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    10

    Default

    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 :)

  4. #4
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default 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 10:26 AM.

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    10

    Default

    Quote Originally Posted by Honopac View Post
    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

    }

    Well...now that Hanopac has just given you the answer lol.
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Sorry for the delayed response.

    Funny thing is that I put the while loop previously, but in the wrong place. I took it out to see what the error was. I honestly thought that I messed up with the scanner import or declaration.

    Thanks for everything.

Similar Threads

  1. problem with file reader
    By Stormrage in forum New To Java
    Replies: 7
    Last Post: 05-16-2010, 12:48 AM
  2. Selecting an input file for Reader
    By myazuid in forum New To Java
    Replies: 3
    Last Post: 11-25-2008, 02:21 AM
  3. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 09:19 AM
  4. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 08:43 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •