Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate local variable(For Loops)

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.

like image 948
user3744056 Avatar asked Jun 16 '14 08:06

user3744056


People also ask

How do you solve duplicate local variables?

In fact the error message even gives you the name of this variable (in the example: a). The solution is to either rename your second (duplicate) variable to something else (like in the example: b) or keep using the existing variable a (by not declaring it again, i.e. lose the second int in this example).

What does it mean to duplicate local variable in Java?

Duplicate local variable means two local variables have the same name. Example. void draw() { int a = 0; // variable a. int a = 0; // duplicate local variable a // solution: rename to b or use the existing a.20-Dec-2012.


4 Answers

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.

like image 196
matteopuc Avatar answered Oct 20 '22 05:10

matteopuc


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.

like image 5
PatNowak Avatar answered Oct 20 '22 06:10

PatNowak


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

like image 3
Stultuske Avatar answered Oct 20 '22 06:10

Stultuske


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++)
like image 3
Revive Avatar answered Oct 20 '22 06:10

Revive