Why won't this see the 'string'.. basic java help.

I am not trying to get someone to do my homework, yes this is indeed homework I just want some help.

errors

bah.java:81: variable anEmail might not have been initialized

if (anEmail==email)// get ignorecase to work

^

bah.java:86: variable anEmail might not have been initialized

email[nEmails++] = anEmail;

^

2 errors

//

import java.io.*;

import java.util.*;

import java.text.*;

import java.lang.*;

publicclass bah

{

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;//loop counter

for (i = 0; i < lineFromFile.length(); i++) ;

int s = i - 1;

int e = i + 1;

boolean hasDot =false;

Find:

if (lineFromFile.charAt(i) =='@')

{

//find start

if (s < 0)break Find;//following breaks have the "Find" to break from~~~~~~~~~~~~~~~~~~~~~~~

boolean isvalid =false;

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) s--;

elsebreak Find;

//find end

if (e == lineFromFile.length())break Find;//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;

elsebreak Find;

}

//print to file

int nEmails=0;//number of emails

String anEmail;// a email

if (s < i && i < e && hasDot)

anEmail = lineFromFile.substring(s, e + 1);

boolean found =false;// check for matching e-mails

for (i = 0; i < nEmails; i++)

if (anEmail==email[i])// get ignorecase to work

found =true;

if (!found)

{

if (nEmails < email.length)

email[nEmails++] = anEmail;

}

//testing defaults disregard to be formatted later

System.out.println(inputFilename);

System.out.println(outputFilename);

System.out.println(nEmails);

}// main

}// public class

Message was edited by:

Scustoms

[7032 byte] By [Scustomsa] at [2007-11-27 5:10:55]
# 1
String anEmail = lineFromFile.substring(s, e + 1);This line is not in your code. Make sure the file you are working on is the same as the file you are trying to compile.Changed your error messages on me I see.Message was edited by: flounder
floundera at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 2

Couple of changes. I've prefaced my comments with a //!!:

//!! add the = "" so that the variable is initialized.

String anEmail = "";// a email

if (s < i && i < e && hasDot)

//!! since this is in an if statement, it may not be true, and the line below

//!! may never be called to

//!! initialize anEmail. Thus you need the anEmail = "" above.

anEmail = lineFromFile.substring(s, e + 1);

boolean found = false; // check for matching e-mails

for (i = 0; i < nEmails; i++)

//!! change this: if (anEmail == email[i]) to the format below.

//!! == doesn't work for strings or objects, only primatives.

if (anEmail.equalsIgnoreCase(email[i]))

{

found = true;

}

petes1234a at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 3

Ok so i don't want to create a new thread.. but once i compile the following code i get the following error. One I haven't seen before.

!!Code in following post!! Won't let me add in a 'edit'!!!

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind

ex out of range: 0

at java.lang.String.charAt(String.java:687)

at bah.main(bah.java:49)

Message was edited by:

Scustoms

Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 4

//

import java.io.*;

import java.util.*;

import java.text.*;

import java.lang.*;

public class bah

{

public static void 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; //loop counter

for (i = 0; i < lineFromFile.length(); i++) ;

int s = i - 1;

int e = i + 1;

boolean hasDot = false;

Find:

if (lineFromFile.charAt(i) == '@')

{

//find start

if (s < 0) break Find; //following breaks have the "Find" to break from~~~~~~~~~~~~~~~~~~~~~~~

boolean isvalid = false;

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) s--;

else break Find;

//find end

if (e == lineFromFile.length()) break Find;//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 Find;

}

//print to file

int nEmails=0;//number of emails

String anEmail = "";// a email

if (s < i && i < e && hasDot)

anEmail = lineFromFile.substring(s, e + 1);

boolean found = false; // check for matching e-mails

for (i = 0; i < nEmails; i++)

if (anEmail.equalsIgnoreCase(email[i]))// get ignorecase to work

{

found = true;

}

if (!found)

{

if (nEmails < email.length)

email[nEmails++] = anEmail;

}

//testing defaults disregard to be formatted later

System.out.println(inputFilename);

System.out.println(outputFilename);

System.out.println(nEmails);

} // main

}// public class

Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 5
> Ok so i don't want to create a new thread.. but once> i compile the following code i get the following> error. One I haven't seen before.> did my previous recommendations help? If so, please say so. If not, please say why not.
petes1234a at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 6
Oh yeah sorry i edited my post when i said thanks! I can't believe how fast you replied and I also hit my head on the wall a few time for not remembering the ""; to initialize the string. It complied but now i get the above error? Appreciate it for sure.
Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 7
That's because a string "" has no chars so there isn't a char at position 0 or any other position. Before you go using chatAt perhaps you should check the length of the String is > 0.
floundera at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 8

your for loop is broken.

for (i = 0; i < lineFromFile.length(); i++) ;

for loops don't have semicolons after them, they have curly braces that enclose the code that is to be looped. the semicolon at the end makes it loop an empty statement.

Also, please get rid of all the "breaks" as they are surely a sign of poorly written code (exceptions exist such as switch / case statements).

good luck

petes1234a at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 9
They are using break for a specific reason, to break back to Find (forget the name for it).
floundera at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 10
> They are using break for a specific reason, to break> back to Find (forget the name for it).Flounder, I agree, but would you implement it this way?
petes1234a at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 11

Hmm we were instructed to used the break statement but in the following format, the 'find' was something i worked in to umm cover myself somewhere else where I couldn't get it to work. The code i was trying to get to work with all the above implementation is this, but it doesn't want to break and returns these errors.

bah2.java:52: break outside switch or loop

if (s < 0) break; //following breaks have the "

Find" to break from~~~~~~~~~~~~~~~~~~~~~~~

^

bah2.java:59: break outside switch or loop

else break;

^

bah2.java:62: break outside switch or loop

if (e == lineFromFile.length()) break;//chang

ed from

^

bah2.java:70: break outside switch or loop

else break;

//

import java.io.*;

import java.util.*;

import java.text.*;

import java.lang.*;

public class bah2

{

public static void 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; //loop counter

int s = i - 1;

int e = i + 1;

boolean hasDot = false;

for (i = 0; i < lineFromFile.length(); i++) ;

{

if (lineFromFile.charAt(i) == '@')

{

//find start

if (s < 0) break; //following breaks have the "Find" to break from~~~~~~~~~~~~~~~~~~~~~~~

boolean isvalid = false;

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) s--;

else break;

//find end

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

int nEmails=0;//number of emails

String anEmail = "";// a email

if (s < i && i < e && hasDot)

anEmail = lineFromFile.substring(s, e + 1);

boolean found = false; // check for matching e-mails

for (i = 0; i < nEmails; i++)

if (anEmail.equalsIgnoreCase(email[i]))// get ignorecase to work

{

found = true;

}

if (!found)

{

if (nEmails < email.length)

email[nEmails++] = anEmail;

}

//testing defaults disregard to be formatted later

System.out.println(inputFilename);

System.out.println(outputFilename);

System.out.println(nEmails);

} // main

}// public class

Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 12
Would that be more appropriate if it worked?
Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 13

> Flounder, I agree, but would you implement it this way?

I haven't looked closely at the code to see what they are trying to achieve. So cannot comment on if this is good or bad in this situation. Just clarifying theat using breaks that are not in a switch statement can be good programming IF the situation needs it.

You haven't fixed the problem mentioned in reply #8.

floundera at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 14

Oops thats fustrating lol thought i didn't ! My bad. Ok so anyhow here is how i stand and now its giving me this error.

bah2.java:44: variable i might not have been initialized

int s = i - 1;

^

1 error

//

import java.io.*;

import java.util.*;

import java.text.*;

import java.lang.*;

public class bah2

{

public static void 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; //loop counter

int s = i - 1;

int e = i + 1;

boolean hasDot = false;

for (i = 0; i < lineFromFile.length(); i++)

{

if (lineFromFile.charAt(i) == '@')

{

//find start

if (s < 0) break; //following breaks have the "Find" to break from~~~~~~~~~~~~~~~~~~~~~~~

boolean isvalid = false;

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) s--;

else break;

//find end

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

int nEmails=0;//number of emails

String anEmail = "";// a email

if (s < i && i < e && hasDot)

anEmail = lineFromFile.substring(s, e + 1);

boolean found = false; // check for matching e-mails

for (i = 0; i < nEmails; i++)

if (anEmail.equalsIgnoreCase(email[i]))// get ignorecase to work

{

found = true;

}

if (!found)

{

if (nEmails < email.length)

email[nEmails++] = anEmail;

}

//testing defaults disregard to be formatted later

System.out.println(inputFilename);

System.out.println(outputFilename);

System.out.println(nEmails);

} // main

}// public class

Scustomsa at 2007-7-12 10:31:10 > top of Java-index,Java Essentials,Java Programming...
# 15

Look at this code:

int i; //loop counter

int s = i - 1;

int e = i + 1;

What does "i" equal? well, possibly 0 if compiler initializes it to 0, but who really knows since i has not been given any value.

Another overgeneralization here: I try not to play with my loop indices before a for loop. I try to create an int variable in the for statement itself (for (int i = 0....) and not set it to anything inside the loop or outside (well I can't if created this way since it is local only to the loop). Of course there are exceptions, but it may help prevent errors like this one.

Good luck.

petes1234a at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...
# 16
int i;int s = i - 1; Error messages are there for a reason. What value do you thing s will have?
floundera at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...
# 17
anyways, flounder is a lot smarter at this than i am. since he's involved, i'm bowing out. again, good luck.
petes1234a at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...
# 18
firstly you have to initialize variable or you have to write else part and assign any value for anEmail variableString anEmail="";orif (s < i && i < e && hasDot)anEmail = lineFromFile.substring(s, e +
Suraweeraa at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...
# 19
Thanks for that Suraweera but that problem was fixed over an hour ago.
floundera at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...
# 20

Pete1234

No need to bow out input is helpful, either way thanks mucho I appreciate it!

Flounder

I initialized i to 0 and i get some input I am going to go offline for a bit and beat myhead against the wall, i don't want to lean on anyone to much. I will be working on this tomorrow if i can't get it squared away.

All

I appreciate it, awesome forum. Thanks!

Scustomsa at 2007-7-21 21:21:24 > top of Java-index,Java Essentials,Java Programming...