Contempt
Contempt

Reputation: 189

How to change user's input as integer to another integer?

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?

Upvotes: 0

Views: 491

Answers (6)

Achintya Jha
Achintya Jha

Reputation: 12843

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.

Upvotes: 0

Thirler
Thirler

Reputation: 20760

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;

Upvotes: 1

Aleksander Gralak
Aleksander Gralak

Reputation: 1509

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

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15644

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.

Upvotes: 3

Fyre
Fyre

Reputation: 1180

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

Upvotes: 1

someone
someone

Reputation: 6572

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;

Upvotes: 1

Related Questions