r/learnjava • • 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

2 Upvotes

4 comments sorted by

5

u/deltageek Feb 28 '20

Looks like you're outputting an extra null there

-14

u/[deleted] Feb 28 '20

looks like you can't help me

3

u/deltageek Feb 28 '20

How exactly did you come to that conclusion? I pointed out what was causing your test to fail. If you wanted more than that, your should have actually asked a question instead of just throwing your work at us and expecting us to do it for you.

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.