Search code examples
javastringsplitnumberformatexception

split in java causing "java.lang.NumberFormatException: For input string: "


Here I have written a piece of code

try {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("vectorValues.txt"));
        HashMap tokenVector = new HashMap();
        while(true){
            System.out.println("Enter word : ");
            word = scan.next(); //giving input normal word
            System.out.println("Enter Vector values <happy,sad,angry,surprise> :");             
            vec = scan.nextLine(); //giving input as vector : "8 5 6 7"
            firstLine = vec.split(" ");
            //Here I am trying to print
            for(String s:firstLine)
                System.out.println("Splitted part : "+s);
            hap = Integer.parseInt(firstLine[0]); 
            sad = Integer.parseInt(firstLine[1]);
            ang = Integer.parseInt(firstLine[2]);
            sur = Integer.parseInt(firstLine[3]); 

            tokenVector.put(word, new Vector(hap,sad,ang,sur));

            System.out.println("Enter more : (0 for no) : ");
            if(scan.nextInt()==0)
                break;
        }
        os.writeObject(tokenVector);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

Vector class is here

class Vector implements Serializable{
    int happy,sad,angry,surprise;
    public Vector(int hap,int sad,int ang,int sur){
        happy = hap;
        this.sad = sad;
        angry = ang;
        surprise = sur;
    }
    public String toString(){
        return ""+happy+" "+sad+" "+angry+" "+surprise+"\n";
    }
}

This is producing following error

Enter word : 
4 5 6 7
Enter Vector values <happy,sad,angry,surprise> :
Splitted part : 
Splitted part : 5
Splitted part : 6
Splitted part : 7
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at FileHandler.inputIntoFile(FileHandler.java:35)
    at FileHandler.main(FileHandler.java:13)

The word is getting accepted using next() but it is not waiting for nextLine() to input into variable vec(). Has it taken '\n' character from the previous standard input while next() and directly moved to next line , thereby storing only blank string into vec which is leading a NumberFormatException ?


Solution

  • The problem might be that you aren't eating up the \n character in your first statement:

     System.out.println("Enter word : ");
                word = scan.next(); //try nextLine() here
                System.out.println("Enter Vector values <happy,sad,angry,surprise> :");             
                vec = scan.nextLine(); //giving input as "8 5 6 7"
                firstLine = vec.split(" ");
    

    Try nextLine() for your first input.

    word = scan.nextLine();
    

    Or put nextLine() after like:

    word = scan.next();
    scan.nextLine();
    

    After your nextInt() put this:

    if(scan.nextInt()==0)
                    break;
    scan.nextLine();