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

[10881 byte] By [Scustomsa] at [2007-11-27 5:20:43]
# 1
File one that worksLast Name First Name Extension Email Address Division DepartmentBach Dan (925) 685-1230 x2800 DBach@dvc.edu Math and Computer Science MathematicsBrecha Jane (925) 685-1230 x2801 JBrecha@dvc.eduMessage was edited by: Scustoms
Scustomsa at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 2

File that doesn't work

<td><font face='Verdana' size='1'><a href='mailto:DBach@dvc.edu'>DBach@dvc.edu</font></td>

<td><font face='Verdana' size='1'>Math and Computer Science</font></td>

<td><font face='Verdana' size='1'>Mathematics</font></td>

</tr>

<tr>

<td><font face='Verdana' size='1'>Brecha</font></td>

<td><font face='Verdana' size='1'>Jane</font></td>

<td><font face='Verdana' size='1'>(925) 685-1230 x2801</font></td>

<td><font face='Verdana' size='1'><a href='mailto:JBrecha@dvc.edu'>JBrecha@dvc.edu</font></td>

<td><font face='Verdana' size='1'>Math and Computer Science</font></td>

<td><font face='Verdana' size='1'>Mathematics</font></td>

</tr>

<tr>

<td><font face='Verdana' size='1'>Burns</font></td>

<td><font face='Verdana' size='1'>Robert</font></td>

<td><font face='Verdana' size='1'>(925) 685-1230 x2610</font></td>

<td><font face='Verdana' size='1'><a href='mailto:RBurns@dvc.edu'>RBurns@dvc.edu</font></td>

<td><font face='Verdana' size='1'>Math and Computer

Message was edited by:

Scustoms

Message was edited by:

Scustoms

Scustomsa at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 3
bueller?
Scustomsa at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> bueller?

At present you are asking for way more of a time and effort commitment

then I believe you are likely to get on any internet forum and perhaps

especially this one.

You need to boil your problem down to more specifics and less reams

of code and sample documents.

Like a few lines of code, perhaps a stack trace or failing that specifics

of what you expect vs what is happening and one or two lines of each

sample document.

cotton.ma at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 5
Can I add one thing? Parsing HTML for data is a right royal PITA.Is it in any way possible for you to not do this?
cotton.ma at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 6
I wish, unfortunately this is the file the instructor is using to test the program.
Scustomsa at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...
# 7

Is there any chance of you using some regex here? Even *gasp*

StringTokenizer would be better than all that charAt stuff.

I think you also look to put more tracing (System.out.println s) in the

sections of code with all the ifs etc. See exactly where it's going wrong.

I suspect the problem is the lack of spaces around the emails in the

second. I could be wrong though.

cotton.ma at 2007-7-12 11:45:14 > top of Java-index,Java Essentials,Java Programming...