-2

As I run this program, when the string "How are you today?" is inputted into the console, instead of being able to type in the Key Matrix into the console after that, the code seems to skip straight to test.process(); which gives me these errors:

Exception in thread "main" java.lang.NumberFormatException: For input string: " are you today?"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.parseInt(Integer.java:615)
at EncoderAdd.stringtoKey(EncoderAdd.java:24)
at EncoderAdd.process(EncoderAdd.java:93)
at Runner.main(Runner.java:19)

The methods that the errors point to are here:

System.out.println("Type message to be encoded here:");
            String encode = in.next();
            if (encode.length() > 0){
                System.out.println("Type key matrix here (Like: k1, k2, k3,  k4):");
                String kmatrix = in.nextLine();
                System.out.println("");
                EncoderAdd test = new EncoderAdd(encode, kmatrix);
                test.process();
            }

public void stringtoKey(){
    String[] temp = mat.split(",");
    for (int i = 0; i < temp.length; i++){
        keyMat[i] = Integer.parseInt(temp[i]);
    }
}

public void process(){
    stringtoKey();
    stringtoInt();
    addkeytoInt();
    intArrtoFinal();
}

How do I fix the errors that are occurring? Thank you!

PallyP
  • 37
  • 8
  • How about checking Yourself in this simple case? Add console printout between lines "for (int i = 0; i < temp.length; i++){" and "keyMat[i] = Integer.parseInt(temp[i]);" and check if You are not passing something like "k1" to parser... – brainovergrow Jan 25 '15 at 11:05
  • How did you define the "in" object? – Ofer Lando Jan 25 '15 at 11:06

1 Answers1

3

Don't mix next() (consumes up to space) and nextLine(). I think the easiest fix is something like

String encode = in.nextLine();

In your posted code next() gets "How", and that leaves " are you today?" for the next call to nextLine().

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226