r/learnjava • u/[deleted] • Feb 28 '20
JetBrains testing thing annoying me again...
Description
At this stage, the program should read an unlimited sequence of numbers from the standard input and calculate their sum. Also, add a /help command to print some information about the program. If you encounter an empty line, do not output anything.
Input/Output example
The example below shows input and the corresponding output. Your program should work in the same way.
4 5 -2 3
10
4 7
11
6
6
/help
The program calculates the sum of numbers
/exit
Bye!
private static int adding(String... nums) {
int sum = 0;
for (String s : nums) {
sum += Integer.parseInt(s);
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String[] arrStr = scanner.nextLine().split(" ");
if (arrStr[0].equals("/exit")) {
System.out.println("Bye!");
break;
} else if (arrStr[0].equals("/help")) {
System.out.println("The program calculates the sum of numbers");
} else if (arrStr.length == 1) {
System.out.println(arrStr[0]);
} else {
System.out.println(adding(arrStr));
}
}
}}
Testing thing saying:
Wrong answer in test #2
Please find below the output of your program during this failed test. Note that the '>' character indicates the beginning of the input line.
17 9
26
-2 5
3
7
7
/exit
Bye!
null
1
u/warpedspockclone Feb 28 '20
You aren't handling the blank line case. Only after that should you handle the adding case with an else.
5
u/deltageek Feb 28 '20
Looks like you're outputting an extra null there