OK so what I want to do is create an infinite loop scanner that will add up all the values from the previously entered values which will then produce after each entered number the total value. How do I do this?

## Recommended Answers

``````while (true)
``````

there is your infinite loop, but trust me, an infinite loop is the last thing you want in running code.

The following piece of code will result in a compile-time error:

``````BigInteger c = a.nextBigInteger();
sum += c;
``````

If the intent is to add BigIntegers then `sum` should be a BigInteger too.

Here's a fix:

``````BigInteger sum = BigInteger.ZERO;

// ...

sum …``````

## All 5 Replies

``````while (true)
``````

there is your infinite loop, but trust me, an infinite loop is the last thing you want in running code.

So what loop do you suggest I use?

Why exactly you need infinite loop to take input.You should atleast have an exit criteria or number of input value and then take those many and calculate on that.

``````Scanner a=new Scanner(System.in);
int b=a.nextInt();
int sum =0;
while(b>0)
{
BigInteger c=a.nextBigInteger();
sum+ = c;
b--;
}
``````

Or exit when input is zero :-

``````Scanner a=new Scanner(System.in);
int b;
int sum =0;
while((b=a.nextInt())>0)
{int count=0;
Integer c=b;
sum+=c;
}
``````

You should never move on to infinite loop as it will keep on using resources and you have to hault the process to stop it.

The following piece of code will result in a compile-time error:

``````BigInteger c = a.nextBigInteger();
sum += c;
``````

If the intent is to add BigIntegers then `sum` should be a BigInteger too.

Here's a fix:

``````BigInteger sum = BigInteger.ZERO;

// ...

sum = sum.add(a.nextBigInteger()); // assume that 'a' has type java.util.Scanner
``````

Also in my opinion it is more user-friendly if the user does not have to specify the amount of numbers prior to entering them all.
This results in less productivity for the end user of the program. Assume he has to enter a five page list of numbers.
Do you really want him to count how many numbers there are before you accept them as input? What if the user miscounts?
The user would have to enter all numbers again...
To overcome this you can use a sentinel value, that is a value that is not part of the dataset, but can be used to indicate the end of input.

nice point out.

BigInteger sum = BigInteger.ZERO;
// ...
sum = sum.add(a.nextBigInteger()); // assume that 'a' has type java.util.Scanner

``````    Scanner a=new Scanner(System.in);
int b=a.nextInt();
BigInteger sum =BigInteger.ZERO;
while(b>0)
{
BigInteger c=a.nextBigInteger();
sum = sum.add(c); //BigInteger is immutable. Therefore, you can't change sum, you need to reassign the result of the add method to sum
b--;
}
``````

To overcome this you can use a sentinel value, that is a value that is not part of the dataset, but can be used to indicate the end of input

As per this is concerned i have already given alternative to take input until zero is pressed above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.