Integer.parseInt throws format exception, what to do?
Author |
Message |
Justin_
|
Posted: Wed Apr 26, 2006 2:13 pm Post subject: Integer.parseInt throws format exception, what to do? |
|
|
I'm getting a numberFormatExcetion from this code that reads integers from a textfile. The textfile has a new integer on each line, so if you made a textfile for example like this:
That would be what you need to run the program and call it IntegerData.txt.
Here's the exact error:
code: |
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:415)
at java.lang.Integer.parseInt(Integer.java:497)
at Sort.sortIt(MooserBubbleSort.java:31)
at MooserBubbleSort.main(MooserBubbleSort.java:9)
|
Java: |
import java.io.*;
import java.util.*;
public class MooserBubbleSort
{
public static void main (String args []) throws IOException
{
Sort integerTextFile = new Sort ();
integerTextFile. sortIt();
}
}
class Sort
{
private int lengthOfArray = 10;
private int[] numberArray = new int[lengthOfArray ];
private int index = 0;
public void sortIt () throws IOException
{
String inputFile = "IntegerData.txt";
String stringNumber;
BufferedReader input;
int number;
input = new BufferedReader(new FileReader(inputFile ));
do
{
stringNumber = input. readLine();
number = Integer. parseInt(stringNumber );
addNumberToArray (number );
} while (stringNumber != null);
bubbleSort (numberArray, index - 1);
for (int i = 0; i <= lengthOfArray; i++ )
System. out. println(numberArray [i ]);
}
private void bubbleSort (int numbers [], int array_size )
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i-- )
{
for (j = 1; j <= i; j++ )
{
if (numbers [j- 1] > numbers [j ])
{
temp = numbers [j- 1];
numbers [j- 1] = numbers [j ];
numbers [j ] = temp;
}
}
}
}
private void addNumberToArray (int num )
{
if (lengthOfArray == index )
{
lengthOfArray = 2 * lengthOfArray;
int[] temp = new int[lengthOfArray ];
for (int i = 0; i < index; i++ )
temp [i ] = numberArray [i ];
numberArray = temp;
}
numberArray [index ] = num;
index++;
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HellblazerX
![](http://www.plamania.co.kr/shopimages/plmtest/2910040000213.jpg)
|
Posted: Wed Apr 26, 2006 2:29 pm Post subject: (No subject) |
|
|
your gonna have to replace that do while loop with a while loop, because what your program is doing is reading 1 more line than what your file actually has.
so, on your line:
code: | do
{
stringNumber = input.readLine();
number = Integer.parseInt(stringNumber);
addNumberToArray(number);
} while (stringNumber != null);
|
replace that with this:
code: | stringNumber = input.readLine ();
while (stringNumber != null)
{
number = Integer.parseInt (stringNumber);
addNumberToArray (number);
stringNumber = input.readLine ();
} |
also theres another problem i noticed with this line:
code: | for (int i = 0; i <= lengthOfArray; i++) |
you shouldn't have have that <= sign, but rather just a < sign, otherwise you'll be calling on a non existing element. You have to remember that arrays start at 0, so an array with a size of 3 will go 0,1,2. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: Thu Apr 27, 2006 5:00 pm Post subject: (No subject) |
|
|
Thanks Hellblazer |
|
|
|
|
![](images/spacer.gif) |
|
|