1

Im having trouble figuring out why my code doesnt work. First off, Im new to Java so bear with me. The task is: Write a program that reads a sequence of integer inputs and prints the cumulative totals. If the input is 1 7 2 9, the program should print 1 8 10 19.

package lektion05forb;

    import java.util.Scanner;

/**
 *
 * @author Lars
 */
public class P52LoopsC 
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{

    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    double cumulative_sum = 0;
    String output_cumulative_sum= "";

    while (input.hasNextDouble())
    {
        double input_number = input.nextDouble();
        cumulative_sum += input_number;
        output_cumulative_sum += String.format("%s ", String.valueOf(cumulative_sum));
        break;
    }
    input.close();
    System.out.println(output_cumulative_sum);
}

}

When I input a sequence of numbers like 3 4 5 8 2, it returns with 34582, instead of the cumulative sum of the numbers. Can anyone explain why, and how i can fix it so it returns the cumulative total of the sequence?

4
  • what is not working? what are you expecting from the code to output? Sep 28, 2015 at 18:33
  • Did you try debugging your code?
    – il_raffa
    Sep 28, 2015 at 18:35
  • ´@thegauravmahawar - it should output the cumulative sum of a sequence of inputs, but if i input 3 4 7 2 1 it just outputs 34721. Sep 28, 2015 at 18:38
  • First of all, remove the break in your loop. You also don't need to format the sum in every iteration of the while loop. Sep 28, 2015 at 18:44

3 Answers 3

0

Here is the answer with a double

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    double cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Double.parseDouble(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}

Input:

Numbers: 1 7 2 9

Output:

1.0

8.0

10.0

19.0

Here is the, perhaps superior, solution with Integer

public static void main(String[]args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Numbers: ");
    Integer cumulativeSum = 0;

    String line = input.nextLine();
    
    String[] numbers = line.split(" ");
    
    for(String number : numbers){
        cumulativeSum += Integer.parseInt(number);
        System.out.println(cumulativeSum);
    }
    
    input.close();
}

Input:

Numbers: 1 7 2 9

Output:

1

8

10

19

0
0

Remove the breakfrom the while-loop.

0

You'll want to use do-while, because this way, the loop will execute at least once, in the code you have written, the loop never executes, because the input.hasNextDouble() is null, because there was no input from user yet after the initialization. (Actually, the loop does execute in case of the scanner, but is generally considered a bad design to execute while-loop without having the control variable implicitly defined and initialized.)

the code goes like this

do {
        double input_number = input.nextDouble();
        cumulative_sum += input_number;
        output_cumulative_sum += String.format("%s ", String.valueOf(cumulative_sum));

    }  while (input.hasNextDouble());

Also, never put break; inside while statement, this way it will loop only once even if the argument is true after first run, because you break out of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.