0

I'm doing a problem to find a day of the week with Zeller's algorithm.

I have to convert 1 to 13 and 2 to 14. However I receive an error of duplicating local variable. The book shows changing variables with temp variable but it does not work on Eclipse. It shows an error. Here's an example:

System.out.println("Enter a month 1-12: ");
int m = input.nextInt();
// Convert January to 13 and February to 14; Zeller's requirement
if (m == 1){
int temp = 13;
int m = temp;
}
else if (m == 2){
int temp = 14;
int m = temp;
}

}

I just started introductory book on Java and maybe there is simple solution here?

1
  • You have declared m multiple times. Each new variable should have unique name... Commented Dec 7, 2012 at 10:26

6 Answers 6

3

This is a simple and smart solution :

System.out.println("Enter a month 1-12: ");
int m = input.nextInt();
m = m + 12;

Also the error you were getting was because of the two variables ( m ) with same name.

You cannot have two variables with same name inside a single block in Java.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah... it's simple now. Just add 12:) Thank you. Trying to solve all problems I see and it gets interesting. Java is cool. :) Thank you!
1

Although the other answers are right (you have to think about where the variables are used, and only at their first creation place int in front of it, if you want to use more, readup on 'scope of a variable').

I would like to provide an extra tip against reusing variables. You could for instance do this:

System.out.println("Enter a month 1-12: ");
int inputMonth = input.nextInt();

// This will hold our result.
int convertedMonth; 
// Convert January to 13 and February to 14; Zeller's requirement
if (inputMonth == 1){
   convertedMonth = 13;
}
else if (inputMonth == 2){
   convertedMonth = 14;
}
else{
   convertedMonth = inputMonth;
}

This way the name of a variable always says what is stored in it. As opposed to m at some point having the user input, and later having the converted input. (Instead of the last else in my example you could decide to start off by setting the default of convertedMonth right away:

int convertedMounth = inputMonth;

1 Comment

Thank you for tips and comprehensive explanation. :]
1

Try this out. You dont need to declare again int m its already in declared and initialized your code as int m = input.nextInt();

System.out.println("Enter a month 1-12: ");
int m = input.nextInt();
// Convert January to 13 and February to 14; Zeller's requirement

if (m == 1){
int temp = 13;
m = temp;
}
 else if (m == 2){
int temp = 14;
 m = temp;

2 Comments

Why my approach did not work? Why int temp is forbidden while declaring int temp outside if/else if block does not throw an error?
You can declared int temp in both if and else statement. It should compile. But you cant do that to int m because its already declared above the if else
1

Declare your variables temp and m outside the if else condition. It seems that the variable is getting declared two times.

Comments

0

Just skip int keyword coresponding to m inside if/else statement.

Comments

0

You can't declare variable with same name more than once in your code. You are declaring "m" again and again instead of assigning value in it you are declaring and assigning . Avoid this type of errors in future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.