Help troubleshooting..
Ok so this is HW no I don't want you to do my hw, i have done most of it with a little help here and there. I am tasked with writing a program that will pull e-mails from a text file. I wrote the program and it works with one text file but not the other. I will post the code, then the text files. Hopefully someone can help me out.
//
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
publicclass Email
{
publicstaticvoid main(String[] args)throws Exception
{
//initialize keyboard reader
BufferedReader cin;
cin =new BufferedReader(new InputStreamReader(System.in));
//initialize variables
int i,i2 = 0;//loop counters
int s;// start char or index of a email
int e;// end char or index of a email
int nEmails=0;//number of emails
String anEmail ="";// a email from substring( s, e)
String inputFilename;// file to be read
String outputFilename;// file to be created
String defaultIFilename ="fileContainingEmails.txt";// default file
String defaultOFilename ="copyPasteMyEmails.txt";// default file
boolean hasDot =false;//verify a dot after '@' symbol
boolean isvalid;//check for valid email characters
//initialize array to 1000 places
String[] email =new String[1000];
//Collect inputfile name or default
System.out.println("What is file name to be read? (enter for default " + defaultIFilename +")");
inputFilename = cin.readLine();
if (inputFilename.length() == 0)// if user hits enter utilize default filename
inputFilename = defaultIFilename;
//Collect outputfile name or default
System.out.println("What is file name to be created? (enter for default " + defaultOFilename+")");
outputFilename = cin.readLine();
if (outputFilename.length() == 0)//if user hits enter utilize default filename
outputFilename = defaultOFilename;
// open a file for input
BufferedReader fin;
fin =new BufferedReader(new FileReader(inputFilename));
String lineFromFile;//= fin.readLine();// read line from file
while ((lineFromFile = fin.readLine()) !=null)// check for end of file
{
for (i = 0; i < lineFromFile.length(); i++)
{
if (lineFromFile.charAt(i) =='@')//parse string and find @
{
//find start of email string
s = i;//start of e-mail
isvalid =true;
while (isvalid)//go left and check each character looking for invalid characters to break
{
isvalid =false;
s--;// go left
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;
}//while find s
s++;// go back one from the invalid character
//find end of email string
e = i;// end of e-mail
isvalid =false;
while (isvalid)
{
isvalid =false;
e++;// go right
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;
}//while find e
//print to file
if (s < i && i < e && hasDot)// if meets all requirements save to temp location
{
anEmail = lineFromFile.substring(s, e);// create anEmail string from substring
}
boolean found =false;// check for matching e-mails
for (i2 = 0; i2 < nEmails; i2++)
{
if (anEmail.equalsIgnoreCase(email[i2]))//check for duplicates
{
found =true;
}
}
if (!found)// if its not found by add to array
{
if (nEmails < email.length)// add to array but do not exceed the size of the array specified above
{
email[nEmails++] = anEmail;// add to array
}
}
}//if statement
}//for loop
}//while
//new file writter
if (nEmails == 0)
System.out.println("No Emails were found!");
else
{ PrintWriter fout;
fout =new PrintWriter(new FileWriter(outputFilename));//create file
System.out.println();//blank line
for (i = 0; i < nEmails-1 ; i++)// print array for number collected
{
System.out.print(email[i] +"; ");//print e-mail to screen with ";" separator
fout.print(email[i] +";");//print to file
}
System.out.print(email[nEmails-1]);//Print last email w/o ";"
//Output number of e-mails to user
System.out.println();//blank line
System.out.println();//blank line
System.out.println(nEmails +" EMAILS COLLECTED AND SAVED TO " +outputFilename+"!");// print out number of emails collected
//close file and close file writter
fin.close();
fout.close();
}//else
}// main
}// public class

