Help running program till end of file.
I am trying to get the below program to run until the end of the file. I was trying to utilize a 'while statement' and
if(!fin,ready())break;
but i can't get it to work? Can some one help me implement this?
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
publicclass bah2
{
publicstaticvoid main(String[] args)throws Exception
{
//initialize keyboard reader
BufferedReader cin;
cin =new BufferedReader(new InputStreamReader(System.in));
//initialize strings
String inputFilename;
String outputFilename;
String defaultFilename ="file.txt";
//initialize array
String[] email =new String[1000];
//Collect inputfile name or default
System.out.println("What is file name to be read? (enter for default)");
inputFilename = cin.readLine();
if (inputFilename.length() == 0)
inputFilename = defaultFilename;
//Collect outputfile name or default
System.out.println("What is file name to be created? (enter for default)");
outputFilename = cin.readLine();
if (outputFilename.length() == 0)
outputFilename = defaultFilename;
// open a file for input
BufferedReader fin;
fin =new BufferedReader(new FileReader(inputFilename));
String lineFromFile = fin.readLine();
int i,i2 = 0;//loop counter
int s;
int e;
boolean hasDot =false;
boolean isvalid;
int nEmails=0;//number of emails
String anEmail ="";// a email
for (i = 0; i < lineFromFile.length(); i++)
{
if (lineFromFile.charAt(i) =='@')
{
s = i;
isvalid =true;
while (isvalid)
{
isvalid =false;
s--;
if (lineFromFile.charAt(s) >='A' && lineFromFile.charAt(s) <='Z') isvalid =true;
if (lineFromFile.charAt(s) >='a' && lineFromFile.charAt(s) <='z') isvalid =true;
if (lineFromFile.charAt(s) =='.') isvalid =true;
if (lineFromFile.charAt(s) >='-') isvalid =true;
if (!isvalid)break;
}
s++;
//System.out.println("Start of email address: "+s+" and the char is "+lineFromFile.charAt(s));
//find end
e = i;
isvalid =true;
while (isvalid)
{
isvalid =false;
e++;
if (lineFromFile.charAt(e) >='A' && lineFromFile.charAt(e) <='Z') isvalid =true;
if (lineFromFile.charAt(e) >='a' && lineFromFile.charAt(e) <='z') isvalid =true;
if (lineFromFile.charAt(e) =='.') isvalid =true;
if (lineFromFile.charAt(e) >='-') isvalid =true;
if (lineFromFile.charAt(e) =='.') hasDot =true;
if (!isvalid)break;
}
//System.out.println("End of email address: "+e+" and the char is "+lineFromFile.charAt(e));
//System.out.println(lineFromFile.substring(s, e));
/*if (e == lineFromFile.length()) break;//changed from
boolean valid = false;
if (lineFromFile.charAt(e) >= 'A' && lineFromFile.charAt(e) <= 'Z') valid = true;
if (lineFromFile.charAt(e) >= 'a' && lineFromFile.charAt(e) <= 'z') valid = true;
if (lineFromFile.charAt(e) == '.') valid = true;
if (lineFromFile.charAt(e) >= '-') valid = true;
if (valid) e++;
if (lineFromFile.charAt(e) == '.') hasDot = true;
else break; */
//print to file
if (s < i && i < e && hasDot)
{
anEmail = lineFromFile.substring(s, e);
System.out.println("Hello, here is what anEmail = "+anEmail);
}
boolean found =false;// check for matching e-mails
for (i2 = 0; i2 < nEmails; i2++)
{
if (anEmail.equalsIgnoreCase(email[i2]))
{
found =true;
}
}
if (!found)
{
if (nEmails < email.length)
{
email[nEmails] = anEmail +" ;";
nEmails++;
}
}
//testing defaults disregard to be formatted later
//System.out.println(inputFilename);
//System.out.println(outputFilename);
//System.out.println(nEmails);
//System.out.println("current line: "+lineFromFile);
//System.out.println("current line length: "+lineFromFile.length());
}//if statement
}//for loop
for (i = 0; i < 10 ; i++)
System.out.println(email[i]);
}// main
}// public class

