Question

I have been working at this for a while and I keep getting the same error.

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.valueOf(Integer.java:582) at StarsTable.addArray(StarsTable.java:50) at StarsTable.<init>(StarsTable.java:24) at Stars.main(Stars.java:7)

Stars.java

public class Stars 
{
    public static void main(String args[])
    {
        String inFile = args[0];
        StarsTable stars = new StarsTable(inFile); line 7
        System.out.println("Program to output a star map.");
        System.out.println("Written by ");
        System.out.println(stars.title());
        Integer[][] a = stars.getArray();
    System.out.print("-");
    for (int x = 0; x < a.length; x ++)
        System.out.print("--");
    for (int i = 0; i < a[0].length; i++)
    {
        System.out.print("\n:");
        for (int j = 0; j < a.length; j++)
        {
            if(stars.checkIfStar(i,j) == 1)
            {
                System.out.print(" *");
            }
            else
                System.out.print("  ");
        }
    }


}

}

StarsTable.java

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;


public class StarsTable 
{
    private ArrayList<Integer> a = new ArrayList<Integer>();
    private String title;
    private String readLine = null;
    private int rows = 0;
    private Integer array[][];

    public StarsTable( String fileName)
    {
        try
        {
            BufferedReader br = new 
                    BufferedReader(new FileReader( fileName));
            title = br.readLine();
            while(( readLine = br.readLine()) != null)
            {
                addArray(readLine); line 24
            }
            br.close();
        }
        catch(IOException e)
        {
            System.err.println("File Not Found. Please "
                    + "enter filename in command line.");
            System.exit(1);
        }


    }

    public String title()
    {
        return title;
    }


    public void addArray(String readLine)
    {
        int i = 0;
        String[] splitLine = readLine.split(" ");
        for(i = 0; i < splitLine.length; i++)
        {
            a.add(Integer.valueOf(splitLine[i])); Line 50
        }
        rows++;
    }

    public Integer[][] getArray()
    {
        toArray();
        return array;
    }

    public void toArray()
    {
        array = new Integer[a.size() / rows][rows];
        int g = 0;
        for (int i = 0; i < (a.size() / rows); i++)
        {
            for (int k = 0; k < rows; k++)
            {
                array[i][k] = a.get(g);             
                g++;
            }
        }
    }
    public int checkIfStar(int i, int k)
    {
        Integer check = 0;
        // Corners
        if (i == 0 && k == 0)
            check = array[i][k] + array[i + 1][k] + array[i][k + 1];
        else if (i == array[0].length && k == 0)
            check = array[i][k] + array[i - 1][k] + array[i][k-1];
        else if (i == 0 && k == array.length)
            check = array[i][k] + array[i][k - 1] + array[i + 1][k];
        else if (i == array[0].length && k == array.length)
            check = array[i][k] + array[i][k - 1] + array[i - 1][k];
        // Edges
        else if (i == 0) //Top
            check = array[i][k] + array[i][k + 1] + array[i - 1][k] + array[i + 1][k];    
        else if (k == 0) // Left
            check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i + 1][k];    
        else if (i == array[0].length) //Right
            check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i - 1][k];    
        else if (k == array.length) // Bottom
            check = array[i][k] + array[i][k - 1] + array[i - 1][k] + array[i + 1][k];    
        // Middle
        else
            check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i - 1][k] + array[i + 1][k];
        check = check / 5;
        if (check < 5)
            return 0;
        else
            return 1;
    }
}

I cannot for the life of me figure out what is the problem with it. Any help would be greatly appreciated. Thanks!

Was it helpful?

Solution

Try to change the code like this,

        for (i = 0; i < splitLine.length; i++)
        {
            a.add(Integer.valueOf(splitLine[i].trim()));
        }

instead of

        for (i = 0; i < splitLine.length; i++)
        {
            try
            {
                a.add(Integer.valueOf(splitLine[i].trim()));
            }
            catch (NumberFormatException ex)
            {
                System.out.println("Exception Occurs while formating : " + splitLine[i]);
            }
        }

OTHER TIPS

Contrary to your (seeming) expectations, the value "" has no Integer value; try one of -

if (! splitLine[i].equals("")) {
  a.add(Integer.valueOf(splitLine[i]));
} else {
  a.add(0); // <-- to just add 0.
}

or

// this is slower, and I probably wouldn't do this.
try {
  a.add(Integer.valueOf(splitLine[i]));
} catch (NumberFormatException nfe) {
  nfe.printStackTrace();
}

The simplest solution is to get rid of addArray entirely. Just use a Scanner to read the file, instead of a BufferedReader. Then you can read it number by number using the nextInt() method, and hasNext() to check when the file is finished.

That way, you don't have to read line by line, you don't have to use split to split the line into numbers, and you don't have to parse each number. In effect, you have re-invented three separate wheels in your code; and introduced a bug along the way.

It's always best to know the API, and use the goodies that it gives you.

I think this is what you need:

public void addArray(String readLine)
{
    int i = 0;
    String[] splitLine = readLine.split(" ");
    for(i = 1; i < splitLine.length; i++) // start at 1
    {
        a.add(Integer.valueOf(splitLine[i].trim()));
    }
    rows++;
}

You can write a util function to deal with the exception, such as,

int safeParseInt(String input){
     try{
         return Integer.parseInt(input);
     }catch(Exception e){
      //error log or print
      return 0;
     }
}

String[] strs = line.split(" ");
for(i = 1; i < strs.length; i++)
{
    array.add(safeParseInt(strs[i]));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top