# Sum of numbers code problems

Page 1 of 1

## 2 Replies - 2693 Views - Last Post: 11 December 2009 - 12:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144841&amp;s=3e5cfde7597292b2a9e33ddce5d6e045&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 outlawzero2099

Reputation: 0
• Posts: 9
• Joined: 10-December 09

# Sum of numbers code problems

Posted 10 December 2009 - 11:49 PM

``` import java.util.Scanner;   // needed for the scanner class.

public class SumofAll
{
public static void main(String[] args)
{

int number;		// To use as a counter.
int maxValue;	// Upper limit of numbers.
int total = 0;	// accumulator.

//create a scanner object to input variables.
Scanner keyboard = new Scanner(System.in);

System.out.println("I will show you the sum of a set of "
+ "consecutive numbers.");

System.out.print("Select a positive non-zero number: ");

maxValue = keyboard.nextInt();  // records the user's input as an int.

while (maxValue <= 0)
{
System.out.print("That is not a valid number please try again ");
maxValue = keyboard.nextInt();

}
for (number = 1; number <= maxValue;)
{
number++;

}

System.out.println("The sum of all your numbers is: " + total);

}
}

```

What I want this program to do is make a sum of numbers starting from the number 1. The user will input the ending number so say 5... the sum should be 15 well somehow it's not adding up for me. can somone please point me in the right direction.. I believe my error is in the latter part of the code where I have my for statement

This post has been edited by outlawzero2099: 10 December 2009 - 11:51 PM

Is This A Good Question/Topic? 0

## Replies To: Sum of numbers code problems

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,620
• Joined: 15-May 08

## Re: Sum of numbers code problems

Posted 11 December 2009 - 12:17 AM

I have edited your code and show you where the error was, it was in the use of your for loop and the fact you never change the value of total - see the code, i made a comment in it

```import java.util.Scanner;   // needed for the scanner class.

public class SumOfAll
{
public static void main(String[] args)
{

int number;        // To use as a counter.
int maxValue;    // Upper limit of numbers.
int total = 0;    // accumulator.

//create a scanner object to input variables.
Scanner keyboard = new Scanner(System.in);

System.out.println("I will show you the sum of a set of "
+ "consecutive numbers.");

System.out.print("Select a positive non-zero number: ");

maxValue = keyboard.nextInt();  // records the user's input as an int.

while ( maxValue <= 0 )
{
System.out.print("That is not a valid number please try again ");
maxValue = keyboard.nextInt();

}

for (number = 1; number <= maxValue; number++ )
{
total += number;	// added this line
}

System.out.println("The sum of all your numbers is: " + total);

}
}
```

Hope this helped

### #3 outlawzero2099

Reputation: 0
• Posts: 9
• Joined: 10-December 09

## Re: Sum of numbers code problems

Posted 11 December 2009 - 12:27 AM

bbq, on 10 Dec, 2009 - 11:17 PM, said:

I have edited your code and show you where the error was, it was in the use of your for loop and the fact you never change the value of total - see the code, i made a comment in it

```import java.util.Scanner;   // needed for the scanner class.

public class SumOfAll
{
public static void main(String[] args)
{

int number;        // To use as a counter.
int maxValue;    // Upper limit of numbers.
int total = 0;    // accumulator.

//create a scanner object to input variables.
Scanner keyboard = new Scanner(System.in);

System.out.println("I will show you the sum of a set of "
+ "consecutive numbers.");

System.out.print("Select a positive non-zero number: ");

maxValue = keyboard.nextInt();  // records the user's input as an int.

while ( maxValue <= 0 )
{
System.out.print("That is not a valid number please try again ");
maxValue = keyboard.nextInt();

}

for (number = 1; number <= maxValue; number++ )
{
total += number;	// added this line
}

System.out.println("The sum of all your numbers is: " + total);

}
}
```

Hope this helped

MAN!! thanks I knew it was something small... I can't believe it.. cool thank you