Jump to content

Java Loop Help

Sebastian Kurpiel

What's up guys,

I have to write a code that takes a integers inputted from the user and adds them up in the end.

This is what I have gotten so far.

import java.util.*;
import java.util.Scanner;
public class Sum
{
    public static void main(String[] args)
    {
        int sum, next;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter a negative number like -1. ");
        do
        {
            sum= 0;
            next = keyboard.nextInt();
            while (next >= 0)
            next = keyboard.nextInt();
            sum = sum + next;
        }
}

I was following a sample code that my professor used in class because his lecture notes are unclear.

 

 

Edited by colonel_mortis
Code Tags
Link to comment
Share on other sites

Link to post
Share on other sites

I think you are missing out on a "while" statement.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Xenift said:

I think you are missing out on a "while" statement.

Should there be one added at the end of this? Looking at the last line of code in the picture.

 

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites


 int sum = 0;
 do{
     next = keyboard.nextInt();
     if(next >= 0){
         sum = sum + next;
     }
 }while(next < 0);


I think this will work.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Tech N Gamer said:

 



	 int sum = 0;

	 do{

	     next = keyboard.nextInt();

	     if(next >= 0){

	         sum = sum + next;

	     }

	 }while(next < 0);


 

I think this will work.

I'm running into this error while compiling, my brackets seem in line though. 

2.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

I have ran the snippit of code that you posted and I fixed it up, here it is:

 

    public static void main(String[] args){

    int sum = 0, next = 0;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter a negative number like -1. ");

        do{

            next = keyboard.nextInt();

            sum = sum + next;

        }while(next >= 0);

 

 

 

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Tech N Gamer said:

I have ran the snippit of code that you posted and I fixed it up, here it is:

 

    public static void main(String[] args){

    int sum = 0, next = 0;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter a negative number like -1. ");

        do{

            next = keyboard.nextInt();

            sum = sum + next;

        }while(next >= 0);

 

 

 

Still receiving this error when compiling 

Could it be because we haven't closed the first bracket? 

3.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, add a closing bracket at the end of the file, then tell us if you get any errors.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, dannytech357 said:

Yes, add a closing bracket at the end of the file, then tell us if you get any errors.

Works like a charm, thank you

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, dannytech357 said:

Yes, add a closing bracket at the end of the file, then tell us if you get any errors.

 

2 hours ago, Tech N Gamer said:

I have ran the snippit of code that you posted and I fixed it up, here it is:

 

    public static void main(String[] args){

    int sum = 0, next = 0;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter a negative number like -1. ");

        do{

            next = keyboard.nextInt();

            sum = sum + next;

        }while(next >= 0);

 

 

 

How would I get it to print the sum at the end?

I have tried adding System.out.println(+sum"");

4.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

My code btw,

import java.util.Scanner;
public class Sum{
    public static void main(String[] args){
    int sum = 0, next = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter a negative number like -1. ");
        do{
            next = keyboard.nextInt();
            sum = sum + next;
        }while(next >= 0);
            {if (next >= 0)
            System.out.println("The sum is "+sum".");
            }
        }
}

Link to comment
Share on other sites

Link to post
Share on other sites

Just a few quick things. First of all if you're going to use a sentinel value you have to account for it. As written your program will be off by whatever that value is. Also as it's written you're never going to output your sum line. The value required to exit the do-while is a number less than zero, which will set the next variable to a number less than zero. The if logic you have written tests to see if next is greater than or equal to zero, which it will not be in order to exit the first loop. Also (and this may not be the case always) I was always told by an instructor that they used to harass people that added two values by writing int1 = int1 + int2. I don't know why someone would be made fun of, but whatever. Try writing it as int1 += int2. I did a quick couple edits you can take a peek at. The modifications I did will accept negative numbers other than negative one, so you can change that if you wish, but otherwise I think it functions as you intended.

 

Also it's a better idea when using the scanner to grab a nextLine rather than a nextInt. In this program it's not an issue, but it will be problematic later. Grab the nextLine as a string, and parse it to what you need. I've added that in as well.

 

Hope it helps!

 

int sum = 1, next = 0;
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter a list of positive intgers and when you have reach the desired amount of intgers enter -1. ");
        do {
            String nextString = keyboard.nextLine();
            next = Integer.parseInt(nextString);
            sum += next;
        } while (next != -1);
        {

            System.out.println("The sum is " + sum + ".");

        }
    }

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×