Search code examples
javasumuser-input

How to sum any amount of user inputted numbers from a single line


Trying to figure out how I would take any amount of inputted numbers from a user and add them together

Example user input: 1 2 3 4 Sum = 10

The user can put any amount of numbers in not a specified amount so if he wanted to add 1 2 3 4 5 6 7 8 9 10 11 12 13, it would sum them all up to 91

Thanks for the help in advance.

import java.util.Scanner;

public class test
{
    public static final int SENTINEL = -1;
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        int sum = 0;

        System.out.println("Enter numbers here");
        while (score >= 0) {
            if (score <= -1) {
            score = kb.nextInt();
            sum += score;
            score = 0;
        }
            System.out.println(sum);
    }
  }
}

Thanks to libik for all his time and help, here is the finished code.

import java.util.Scanner;

public class JavaApplication1156 {

    public static void main(String[] args) {
    System.out.println("Enter numbers here");
    int sum;
    do {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        sum = 0;
        String line = kb.nextLine();
        kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
        while (kb.hasNextInt()) {
            score = kb.nextInt();
            sum += score;
        }
        if (sum <= -1)
        System.out.println("Application ended");
        else if (sum >= 0)
        System.out.println("Sum = " + sum);

    } while (sum != -1);
  }

}

Solution

  • It is very easy actually

    import java.util.Scanner;
    
    public class JavaApplication115 {
    
        public static void main(String[] args) {
            System.out.println("write numbers, if you write zero, program ends");
            Scanner input = new Scanner(System.in); //just copy-and paste this line, you dont have to understand it yet.
            int number;
            int sum = 0;
            do {
                number = input.nextInt(); //this reads number from input and store its value in variable number
                sum+= number; //here you add number to the total sum
            } while(number != 0); //just repeat cycle as long as number is not zero
    
            System.out.println("Sum is : " + sum);
        }
    
    }
    

    Working code based on your code :

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        int sum = 0;
    
        System.out.println("Enter numbers here");
        String line = kb.nextLine();
    
        kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
        while (kb.hasNextInt()) {
            score = kb.nextInt();
            sum += score;
        }
        System.out.println(sum);
    }
    

    Also if you are interested in "minimal" version, which is the same as the one before, but using as less code as possible, here it is :

    public static void main(String[] args) {
        int sum = 0;
        System.out.println("Enter numbers here");
        Scanner kb = new Scanner((new Scanner(System.in)).nextLine()); //has to do this to make the kb.hasNexInt() work.
        while (kb.hasNextInt()) {
            sum += kb.nextInt();
        }
        System.out.println(sum);
    }
    

    Find sum of each line as long as sum is not zero (based on second block of code) :

    public static void main(String[] args) {
        System.out.println("Enter numbers here");
        int sum;
        do {
            Scanner kb = new Scanner(System.in);
            int score = 0;
            sum = 0;
            String line = kb.nextLine();
            kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
            while (kb.hasNextInt()) {
                score = kb.nextInt();
                sum += score;
            }
            System.out.println("Sum = " + sum);
        } while (sum != 0);
    }