Reputation: 17
I'm a new programmer and attempted to make a program which adds all user inputted numbers. Here is the code:
import java.util.Scanner;
import java.io.*;
public class Adding
{
private int numOfInt, newInt;
/**
* Constructor for objects of class Adding
*/
public Adding()
{
// initialise instance variables
Scanner console = new Scanner( System.in );
System.out.print("How many integers will be added?");
numOfInt = console.nextInt();
newInt = 0;
}
public int addIntegers()
{
int count = 0;
int sum = 0;
while( count <= numOfInt )
{
System.out.println("The count is: " + count + " and the current sum is: " + sum);
count = count + 1;
Scanner console = new Scanner( System.in );
System.out.println("Enter an integer: ");
newInt = console.nextInt();
sum = sum + newInt;
}
return sum;
}
public void displaySum()
{
System.out.println("the sum is " + this.addIntegers());
}
}
This is the second class for the main:
import java.util.Scanner;
import java.io.*;
public class AddingMain
{
public static void main( String[] args )
{
Adding add = new Adding();
add.addIntegers();
add.displaySum();
}
}
However, the loop repeats twice (as can be seen in the input below, edited to save space) and actually ignores the first set of numbers entered:
How many integers will be added?3
The count is: 0 and the current sum is: 0
Enter an integer: 1
The count is: 1 and the current sum is: 1
Enter an integer: 2
The count is: 2 and the current sum is: 3
Enter an integer: 3
The count is: 3 and the current sum is: 6
Enter an integer: 1
The count is: 0 and the current sum is: 0
Enter an integer: 2
The count is: 1 and the current sum is: 2
Enter an integer: 3
The count is: 2 and the current sum is: 5
Enter an integer: 4
The count is: 3 and the current sum is: 9
Enter an integer: 5
the sum is 14
Can someone explain why this is happening and how to fix it? Thank you!
Upvotes: 0
Views: 2196
Reputation: 2011
The loop repeats twice because you are calling addIntegers()
twice.
The very first time you called addIntegers()
by writing add.addIntegers();
and the next time you called addIntegers()
by writing System.out.println("the sum is " + this.addIntegers());
.
this.addIntegers()
is again calling the method addIntegers()
.
Extra Suggestion
You can use the scanner object by just instantiating the Scanner object at class level i.e you dont need to instantiate scanner object twice in your code.
You can go like this--
public class Adding
{
private int numOfInt, newInt;
Scanner console = new Scanner( System.in );
Upvotes: 4
Reputation: 171
You can remove the addInteger() from the main() , so that it is only called once, and you get your desired output .
Upvotes: 0
Reputation: 31925
add.addIntegers(); // invoke 1st time
add.displaySum(); // invoke 2nd time
You invoked addIntegers()
explicitly once, and it is invoked by displaySum()
the 2nd time
adding integers and display sum are two different responsibilities, you'd better split them into 2 functions. Makes displaySum()
display sum only other than adding integers. You can create an instance variable sum
in Class Adding
, and change displaySum() to
public int displaySum(){
return sum;
}
Upvotes: 0
Reputation: 1424
The reason is that addIntegers
is called twice, one time in the main
and one time in displaySum
. So, the loop is executed twice, too.
Upvotes: 1