0

I have created a program that uses the Scanner to ask the user for int values until they insert -1 which makes the program to stop receiving numbers. After doing so, it will add all the values entered by the user. This is my code so far:

public static void main(String[] args)
{
    int sum = 0, value, count = 0;   
    Scanner scan = new Scanner (System.in);  
    System.out.print ("Enter an integer (-1 to quit): ");  
    value = scan.nextInt(); 
    String string = Integer.toString(value);
    while (value != -1)
    {  
        count = count + 1;  
        sum = sum + value;  
        System.out.print("Enter an integer (-1 to quit): ");  
        value = scan.nextInt();  
        string = Integer.toString(value);
    }  
    System.out.println ();  
    if (count == 0)  
        System.out.println ("No values were entered.");  
    else  
    {  
        System.out.println("Number entered: " + string + ",");
        System.out.println ("The sum is " + sum);  
    }  
}  

I want the output to look like this:

Entered numbers: 1,2,3,4,5 //example of number the user might enter
The sum is 15

I wanted to use a String for it to give me the sets of entered numbers, but it only gives me the last entered value. Which is -1 because that is the number that has to be entered to stop the program.

How can I out this problem?

1
  • No, because that will give me the number of values entered. And I want it to give me the values, not how many values were entered. Commented Sep 26, 2015 at 18:10

3 Answers 3

4

In your

while(value != -1){

    ...
    string = Integer.toString(value);
}

you are replacing string value with new one, so old value is lost. You should add new value to previously stored one. So your code may look like

string = string + "," + value;

You should also place this code before handling value which will be -1.


BTW, when you will learn more about Java you will know that each time you call

string + "," + value

new String is being created. Such string will need to copy content of other chunks. which may be very inefficient in case of long strings. To optimize this we can use StringBuilder and append new parts to it in loop.

In Java 8 we can also use StringJoiner which can automatically add prefixes, delimiters and suffixes for us.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I do not know why I did not think of that. Haha, but thank you.
0

Simply use string += "," + Integer.toString(value); This will give you a comma separated list of all the values you have entered. Your current statement string = Integer.toString(value); causes the variable string to be reset to the string representation of "value" for every iteration.

Comments

0

Check this code.

public static void main(String[] args)
{
   int sum = 0, value, count = 0;   
   Scanner scan = new Scanner (System.in);  
   System.out.print ("Enter an integer (-1 to quit): ");  
   value = scan.nextInt(); 
   StringBuilder sb = new StringBuilder();
   while (value != -1)
   {  
       if(sb.length() == 0){
           sb.append(value);
       }else{
           sb.append(","+value);
       }
      count = count + 1;  
      sum = sum + value;  
      System.out.print("Enter an integer (-1 to quit): ");  
      value = scan.nextInt();  
   }  
   System.out.println ();  
   if (count == 0)  
      System.out.println ("No values were entered.");  
   else  
   {  
      System.out.println("Number entered: " + sb.toString());
      System.out.println ("The sum is " + sum);  
   }  
}  

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.