hgg ohh hgg ohh - 4 months ago 16
Java Question

Trying to calculate a sum from input, when text is thrown in as well

Have input passed through Scanner. It has both numbers and letters and spaces.

Letters being stripped out, leaving only spaces and numbers.

If I input without spaces, it works fine, but if I add spaces, it throws the error:


java.lang.NumberFormatException: For input string: "" (in
java.lang.NumberFormatException)


This is applied against the line

int dataInt = Integer.parseInt(data[i]);


Stderr outputs
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
Program2.main(Program2.java:21)


Code is below

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

public class Program2 {

public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
input = input.replaceAll("[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]","");
System.out.println(input);
while(!input.equals("#")){
String[] data = input.split(" ");
int sum = 0;
if (!input.equals("")){
for(int i = 0; i < data.length; i++){
int dataInt = Integer.parseInt(data[i]);
sum = sum + dataInt;

}

}
System.out.println(sum);
input = kb.nextLine();
}
} //main


} // class Program2

Answer

Turns out that this program is supposed to extract all numbers in each line, and sum them up. Each line is free to be mixed with characters and spaces. Ex: fsdjs 3 8 herlks 983 should produce 944.

There were a few things wrong

if (!input.equals(""))
    for(int i = 0; i < data.length; i++){

will only check if the input is empty, but it should be the array of split up substrings that we should be worried about as that's what we should be operating on. There will be empty strings after calling split(). It should really be

for(int i = 0; i < data.length; i++){
    if (!data[i].equals(""))

While running your code, there seems to be times where the program gets caught up with spaces while calling parseInt(). Not sure how it worked, but it had to do with the number of replaceAll()s.

The string input is basically a list of numbers delimited by a series of alphabets and spaces. You could just split on that with input.split("[^\\d]+) instead of calling replaceAll() multiple times.

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

public class Program2 {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        String input = kb.nextLine();
        System.out.println(input);

        while(!input.equals("#")) {
            //                    VVV
            String[] data = input.split("[^\\d]+");
            int sum = 0;

            for(int i = 0; i < data.length; i++) {
                if (!data[i].equals("")) {
                    int dataInt = Integer.parseInt(data[i]);
                    sum = sum + dataInt;
                }
            }

            System.out.println(sum);
            input = kb.nextLine();
        }
    } //main
} // class Program2