Search code examples
javanumberformatexception

NumberFormatException in Stars.java


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!


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]);
                }
            }