Search code examples
javahashtablenumberformatexception

How do I fix a NumberFormatException (from text file input)?


I was wondering if I could have some help with this NumberFormatException with code using a text input.


The result should be it being able to run properly and be able to first put 50 strings into the hashTable and then remove 10 afterwards.

I have tried placing the removeLine.next() inside a String datatype and then placing the String back inside the Integer.parseInt which didn't work.


Here is the class:

import java.io.*;
import java.util.*;

    public class hashTest {
    public static void main(String args[]) throws FileNotFoundException {
        HashTable hashTable = new HashTable();

        Scanner insert = new Scanner(new File("data1.txt"));

        while(insert.hasNext()) {
            String line = insert.nextLine();

            Scanner insertLine = new Scanner(line);

            insertLine.next();
            insertLine.next();

            int index = Integer.parseInt(insertLine.next());

            String data = insertLine.nextLine();

            hashTable.put(index, data);
        }



        Scanner remove = new Scanner(new File("data2.txt"));

        while(remove.hasNext()) {
            String line = remove.nextLine();

            Scanner removeLine = new Scanner(line);

            removeLine.next();
            removeLine.next();

            int index = Integer.parseInt(removeLine.next());

            hashTable.remove(index);
        }
    }
}

data1.txt :

003 : 68682774 MALIK TULLER
004 : 24248685 FRANCE COELLO
005 : 25428367 DUSTY BANNON
006 : 79430806 MELVINA CORNEJO
007 : 98698743 MALIA HOGSTRUM
008 : 20316453 TOMASA POWANDA
009 : 39977566 CHONG MCOWEN
010 : 86770985 DUSTY CONFER
011 : 92800393 LINNIE GILMAN
012 : 31850991 WANETA DEWEES
013 : 81528001 NEAL HOLSTEGE
014 : 46531276 BRADLY BOMBACI

data2.txt :

92800393 LINNIE GILMAN
86770985 DUSTY CONFER
31850991 WANETA DEWEES
46531276 BRADLY BOMBACI
25428367 DUSTY BANNON
68682774 MALIK TULLER
18088219 PENNY JOTBLAD
48235250 KENNITH GRASSMYER
20316453 TOMASA POWANDA
54920021 TYSON COLBETH
22806858 LAVERNE WOLNIK
32244214 SHEMEKA HALLOWAY
81528001 NEAL HOLSTEGE
24248685 FRANCE COELLO
23331143 JUSTIN ADKIN
79430806 MELVINA CORNEJO
59245514 LESLEE PHIFER
64357276 SCOT PARREIRA
50725704 GENARO QUIDER
52298576 AUDIE UNCAPHER
54657809 MARTY ENOCHS
54526749 TOBI HEATLEY
24903965 ALONSO GILSTAD
84936051 DEONNA STRAZZA
62522327 AHMAD THAYER
90572271 ELIJAH METEVIER
88999386 ISMAEL ELKAN

Solution

  • NumberFormatExceptions with Integer.parseInt() are most often caused by attempting to read something into an int that is not actually an int. Try printing each line as it is read in. If you have a line that is not purely an int (e.g., Hello123), you will get this exception with Integer.parseInt(). A cleaner debugging method (and better coding practice) would be to catch the exception and print the problematic line. You will probably see right away what's causing the issue. When reading text input from anywhere, it's never good to assume that the data is of the format you're expecting.

    When your input contains data other than the int values you need, you can read each line's values into an array and extract the proper value(s). Here's an example of how you might extract the values from a single line in your second data file. Keep in mind that this still makes assumptions about the input format and therefore, is not completely fool-proof.

    try {
        // Split the line by whitespace, saving the values into an array
        String[] singleLineVals = someLine.split("\\s+");
        // Extract the first value
        int firstValue = Integer.parseInt(singleLineVals[0]);
    } catch (NumberFormatException nfe) {
        // Handle the exception
    }