I'm trying to solve an assignment (I'm still very new to Java) and have combed through many resources to solve this conflict but still can't quite work it out.(NOTE: Tuna is my Scanner variable)
int counted, sum, counted1;
System.out.print("Enter your number to be calculated: ");
counted = tuna.nextInt();
counted1 =tuna.nextInt();
for(int counted=0;counted<=counted1;counted++){
System.out.println("The sum is: "+ counted);
}
}
}
Result is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable counted
The problem I am supposed to solve is:
Write a program to read in a number and sum up all the numbers from 1 to that number. For e.g, if
the user key in 6, then the output is 21 (1+2+3+4+5+6).
ADDED: I read a question() that is rather similar but I did not make the smae mistake by declaring it before.
You are declaring two variables with the same name in the same scope: counted
is declared outside the loop and inside the loop. By the way, according to your specification:
Write a program to read in a number and sum up all the numbers from 1 to that number. For e.g, if the user key in 6, then the output is 21 (1+2+3+4+5+6)
you don't need the first counted
var, because it is a constant (the constant 1). You can, so, declare 1 as a constant this way:
final int STARTING_NUMBER = 1
and then use this constant in your loop:
int counted, sum;
counted = tuna.nextInt();
for(int index=STARTING_NUMBER;index<=counted;index++){
sum=sum+index;
}
System.out.println("The sum is: "+ sum);
EDIT: you can declare your variables wherever you want. The important is that you declare them once in the same scope. You can do something like:
int counted, sum, index;
counted = tuna.nextInt();
for(index=STARTING_NUMBER;index<=counted;index++){
sum=sum+index;
}
System.out.println("The sum is: "+ sum);
declaring index
outside the loop. The result won't change. It is a common pratice, though, to declare the variable that the for loop
uses as index (you can call this variable index
or counter
or i
or mySisterIsCool
and so on) inside the for loop itself (in its guard, to be more precise) for a better readability.
In Java (contrary to C++) your local variables cannot have the same name, as variables declared before if they're in the same range f.e.
{ //external variable
int x;
{ //internal variable
int x;
//do something
}
}
You won't be able to compile this code.
In your example you should make it something like that:
int counted, sum;
System.out.print("Enter your number to be calculated: ");
counted = tuna.nextInt();
for(int counter=0;counter<=counted;counter++){
sum=sum+counter;
}
System.out.println("The sum is: "+ sum);
}
The result will be in the variable sum and it will be displayed on your console only once.
for(int counted=0;counted<=counted1;counted++){
System.out.println("The sum is: "+ counted);
}
here you re-declare an int with the exact same name as already exists.
use something like:
for(int counted1=0;counted1<=counted1;counted1++){
System.out.println("The sum is: "+ counted1);
}
or
for(counted=0;counted<=counted1;counted++){
System.out.println("The sum is: "+ counted);
}
if you want to use the previously declared variable
The problem is you have declared the int "counted" twice. Once at the top of the code and once inside the for loop. Removing the int declaration from the for loop should fix this eg:
for(counted=0;counted<=counted1;counted++)
Indeed you declared variable counted
outside the loop and as a loop counter, so these 2 variables with teh same name are in the same block (part of code between {
and }
.
You should either avoid declaring this variable outside the loop (if you need it as a loop counter only) or declare it outside the loop and then use it in loop as following:
for(;counted<=counted1;counted++)
In Java, the scope of local variables must be observed. To fix that, rename the "counted" in the for statement.
As for the problem: check out http://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes