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