Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Integer.parseInt throws format exception, what to do?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: 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:
code:

1
2
3

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++;
    }
}
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: 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.
Justin_




PostPosted: Thu Apr 27, 2006 5:00 pm   Post subject: (No subject)

Thanks Hellblazer
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: