Newbie Needs Array Help

I have a txt file that contains the following:

5:2

1:2

3:3

4:3

2:1

5:3

1:2

2:3

3:2

2:1

I need to make a few calculations to the data above. I cannot change the format of the file. I need the sum of each column and I need to compare the integers in each row. When I compare the two ints I just need a true or false depending on whether row1>row2.

I think I can make these calculations by first splitting the data into two rows. I'm trying to read this data from a text file but when I get to the end of the file I get a NumberFormatException. I've looked over my code for hours but can't seem to find my mistake. This is a piece of my code, any advice?

while((line = inScrn.readLine())!=null) {

scores = line.split(":")

for(int index=0; index < scores.length; index++) {

int num = Integer.parseInt(scores[index]);} //This is where I get the error

System.out.println(num); }

I inserted the System.out.println just to see how much of the file my code reads. It reads all the data but errors at the end of the file. I tried adding empty spaces to the end of my file but that didnt make a difference.

The exact error at the bottom of my compiler is:

NumberFormatException:

for input String: "" (in java.lang.NumberFormatException)

[1370 byte] By [creepa] at [2007-10-2 5:40:12]
# 1

> while((line = inScrn.readLine())!=null) {

> scores = line.split(":")

> for(int index=0; index < scores.length; index++) {

> int num = Integer.parseInt(scores[index]);} //This is where I get the error

> System.out.println(num); }

>

> I inserted the System.out.println just to see how

> much of the file my code reads. It reads all the data

> but errors at the end of the file. I tried adding

> empty spaces to the end of my file but that didnt

> make a difference.

The code you posted isn't the code you're talking about; scores = line.split(":") contains 2 compiler-errors.

> The exact error at the bottom of my compiler is:

> NumberFormatException:

> for input String: "" (in

> java.lang.NumberFormatException)

You're trying to convert a String with length == 0 to an int; that won't work.

Here's an example how to read a textfile using the new (1.5) Scanner class:

import java.util.Scanner;

import java.io.File;

import java.io.IOException;

public class ReadFile {

public static void main(String[] args) throws IOException {

Scanner inFile = new Scanner(new File("test.txt"));

while(inFile.hasNext()) {

String line = inFile.nextLine();

String scores[] = line.split(":");

int num1 = Integer.parseInt(scores[0]);

int num2 = Integer.parseInt(scores[1]);

System.out.println("num1: "+num1+", num2: "+num2);

}

}

}

Good luck.

prometheuzza at 2007-7-16 1:50:32 > top of Java-index,Java Essentials,New To Java...
# 2

> I have a txt file that contains the following:

>

> 5:2

> 1:2

> 3:3

> 4:3

> 2:1

> 5:3

> 1:2

> 2:3

> 3:2

> 2:1

>

> I need to make a few calculations to the data above.

> I cannot change the format of the file. I need the

> sum of each column and I need to compare the integers

> in each row. When I compare the two ints I just need

> a true or false depending on whether row1>row2.

>

> I think I can make these calculations by first

> splitting the data into two rows. I'm trying to read

> this data from a text file but when I get to the end

> of the file I get a NumberFormatException. I've

> looked over my code for hours but can't seem to find

> my mistake. This is a piece of my code, any advice?

>

> while((line = inScrn.readLine())!=null) {

>scores = line.split(":")

>for(int index=0; index < scores.length; index++) {

> int num = Integer.parseInt(scores[index]);}

> ex]);} //This is where I get the error

>System.out.println(num); }

>

> I inserted the System.out.println just to see how

> much of the file my code reads. It reads all the data

> but errors at the end of the file. I tried adding

> empty spaces to the end of my file but that didnt

> make a difference.

>

> The exact error at the bottom of my compiler is:

> NumberFormatException:

> for input String: "" (in

> java.lang.NumberFormatException)

Your Error indicates that java tried to convert "" to a Number

which of course gives NumberFormatException

So I'd guess from that, that your program reads in all the data and does what you require.

But Then it reads in an empty line i.e. ""

You'll want to handle this in some way like:

//.................

if( ! line.length() == 0 ) //Don't try and Interpret a Blank Line at all

{

scores = line.split(".");

//.......... and etc

}

and also think about something like

//............................

int num =0;

try

{

num = Integer.parseInt(scores[index]);

}

catch( NumberFormatException e )

{

System.out.println( "Could Not Parse: " + scores[index] );

}

As for the procedure to do what you want, well, I'm sure you'll get that.

Ah Yeh, much as it catches me too:-

nulland empty String "" are not the same......

WIlfred_Deatha at 2007-7-16 1:50:32 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for the help everyone. For the most part I have my entire code complete but it continues to get an empty string error at the end of my file. I've used printouts to debug and my code does exactly what its supposed to up until it reaches the end of the input file.

I tried "if( ! line.length() == 0 )" but my compiler returns "operator ! cannot be applied to int"

Any suggestions? This is how I'm reading my file.

while((line = inScrn.readLine())!=null) {

String[]scores = line.split(":");

for(int i = 0; i < scores.length; i++){

myTeam = Integer.parseInt(scores[0]);

otherTeam = Integer.parseInt(scores[1]);}

}

creepa at 2007-7-16 1:50:32 > top of Java-index,Java Essentials,New To Java...
# 4

> I tried "if( ! line.length() == 0 )" but my compiler

> returns "operator ! cannot be applied to int"

Because you can't put a '!' operator in front of an int, only a boolean expression. It should be:

if(line.length() != 0) { }

orif(!(line.length() == 0)) { }

Most programmers prefer option 1.

> Any suggestions?

Here's a safer way:

while((line = inScrn.readLine()) != null) {

// Check if there's a ":" in your String. If this

// is the case, then the String.length() is also > 0.

if(line.indexOf(":") != -1) {

// Split the String.

String[] scores = line.split(":");

// Check if the String array holds two

// (and only two) elements.

if(score.length == 2) {

myTeam = Integer.parseInt(scores[0]);

otherTeam = Integer.parseInt(scores[1]);

}

}

}

Good luck.

prometheuzza at 2007-7-16 1:50:32 > top of Java-index,Java Essentials,New To Java...