• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Little help with java

TekSomniaK

[H]ard|Gawd
Joined
Mar 26, 2003
Messages
1,144
Ok so my problem is I'm supposed to read multiple lines from a text file, with each line being passed as a string object, then I'm supposed to use the StringTokenizer class to assign variables to each token in the line, which are separated by commas. Here's two sections of my class

Code:
        private String name;
        private int harris;
        private int usaToday;
        private int[] computerRanking = new int[6];

Code:
        public Team(String line)
        {
                        StringTokenizer st = new StringTokenizer(line, ",");
                        name = st.nextToken();
                        harris = Integer.parseInt(st.nextToken());
                        usaToday = Integer.parseInt(st.nextToken());
                        for (int i = 0; i < computerRanking.length; i++)
                        {
                                computerRanking[i] = Integer.parseInt(st.nextToken());
                        }
        }

And what results is that my integer variables are still 0. Sorry for being a bit noobish and the answer's probably right in front of me but I can't figure it out. If anyone's curious this assignment is to take a text file full of college football teams and determine their rankings, go mizzou baby :p
 
more information would be helpful.

what is an example line from the file?
what debugging steps have you done?
are you sure line read in the data properly?
are you sure the StringTokenizer is returning the tokens you expect before you call parseInt on it?
are you required to use StringTokenizer? its been deprecated in favor of String.split.
 
Check you Java console for any number format exceptions from parseint also.


What does your data line look like? (post an example if possible)


You should also be using hasMoreTokens() just to make sure there are tokens left to read
 
If you're reading lines from a text file, don't you need to use buffered reader and file reader?
 
The code you have seems reasonable. It may be something in the code you didn't show.

I would also change your method to start with a lower case letter team() instead of Team(). Its a java naming convention that classes start with Uppercase letters, while other things start with lower case letters.

I assume you have a new instance of each object you are storing your data in for each line. Otherwise your name,harris,etc would be overwritten each time.
 
What does your data line look like? (post an example if possible)

Each line has the college's name, which I assign to name. Also the stringtokenizer does work when I do not parseint as I've tested it by just leaving them as strings and outputted their values. Here's an example line.

Missouri,2779,1454,25,23,24,25,25,25

Also I do have the buffered and file readers but those are already closed and done with (and I know that they worked) as each line from the text file is now a String passed as an argument to the Team constructor above.(through using an array of Team objects)
 
i agree with ambientZ the code is reasonable, in fact, using the example line you gave, it worked perfectly for me.

sidenote: Team is a class, it looks like he posted a constructor, so should be capitalized.
 
i agree with ambientZ the code is reasonable, in fact, using the example line you gave, it worked perfectly for me.

sidenote: Team is a class, it looks like he posted a constructor, so should be capitalized.

So he did, i missed that :)

and yeah, the code thats included works fine.

Code:
public static void main(String[] args) throws IOException {
	ArrayList<Team> teams = new ArrayList<Team>();
	BufferedReader br = new BufferedReader(new FileReader("teams.txt"));
	String line = null;
	while((line=br.readLine())!=null){
		teams.add(new Team(line));
		System.out.println(teams.get(teams.size()-1));
	}
}

is what i used to test it.
 
It's interesting that they're having you use the StringTokenizer instead of String.split. StringTokenizer has been deprecated for some time now.
 
It's interesting that they're having you use the StringTokenizer instead of String.split. StringTokenizer has been deprecated for some time now.

We're going by the book I guess :confused: and that was published in like 2005

Anyways I figured everything out and finished my program. The problem I was having was absolutely noobish and it ended up being that I needed to typecast the integer instance variables when I used them in methods that returned a double. Needless to say I felt like an idiot.
 
We're going by the book I guess :confused: and that was published in like 2005

Anyways I figured everything out and finished my program. The problem I was having was absolutely noobish and it ended up being that I needed to typecast the integer instance variables when I used them in methods that returned a double. Needless to say I felt like an idiot.

Don't feel dumb -- you figured it out and you know what to do in the future, right?
 
We're going by the book I guess :confused: and that was published in like 2005

Anyways I figured everything out and finished my program. The problem I was having was absolutely noobish and it ended up being that I needed to typecast the integer instance variables when I used them in methods that returned a double. Needless to say I felt like an idiot.

if you ever decide to program for a living, what you went through here will probably happen a lot. small error in logic = hours of debugging.
 
Back
Top