Reputation: 3
Ok so this program is trying to display the middle digit of a 1-4 digit integer and if it has an even number of digits it should display that there is no middle digits, however I can't get it to output the answer properly, here is the code:
public class digit {
public static void main(String[] arg) {
int MyInt1, MyInt2, MyInt3, MyInt4;
EasyReader keyboard = new EasyReader();
// Get the 1-4 digit integer
int MyInt = keyboard.readInt("Enter your integer: ");
// Calculate the middle digit
MyInt1=MyInt/10;
if (MyInt1 < 1) {
System.out.print("The middle digit is: ");
System.out.println(MyInt);
MyInt2=MyInt1/10;
}else if (MyInt2 < 1) {
System.out.println("No middle digit");
MyInt3=MyInt2/10;
}else if (MyInt3 < 1) {
System.out.print("The middle digit is: ");
System.out.println(MyInt2);
MyInt4=MyInt3/10;
}else if (MyInt4 < 1) {
System.out.println("No middle digit");
}
}
}
I also import a package at the start.
An example output I get is if I enter 1234 as the integer I get: '123412No middle digit' as my answer, I must be doing something wrong with the else if statements I think but I don't know what it is. Help is appreciated, thanks
Upvotes: 0
Views: 200
Reputation: 501
Try this code:
Scanner input = new Scanner(System.in);
System.out.print("Give an integer number from 1 to 4 digits: ");
String number = input.next();
switch(input.length())
{
case 3:
System.out.println("The middle is " + input[1]);
break;
case 4:
System.out.println("The middles are " + input[1] + " " + input[2]);
break;
default:
System.out.println("You are inserted wrong number, there aren't middles");
}
Upvotes: -1
Reputation: 41625
public class PrintMiddleDigit {
public static void main(String[] arg) {
EasyReader keyboard = new EasyReader();
int number = keyboard.readInt("Enter your integer: ");
String str = String.valueOf(number);
if (str.length() % 2 == 0) {
System.out.println("No middle digit");
} else {
int middle = str.length() / 2;
System.out.print("The middle digit is: ");
System.out.println(str.charAt(middle));
}
}
}
Some remarks:
UpperCamelCase
, variable names in lowerCamelCase
.str.length() / 2
will always round towards zero.Upvotes: 1
Reputation: 77044
Indentation doesn't define what's executed and what isn't executed with if
statements.
If you don't satisfy the first condition, MyInt2
will never be defined, so the next condition will also fail. In your original code, if MyInt1 < 1
is true, then you will print The middle digit is:
and calculate the value of MyInt2
. If, however, the condition is false you will check if MyInt2 < 1
, but MyInt2
is undefined, since it's only calculated (in your original code) when the first condition is true.
Something like this should work:
// Calculate the middle digit
MyInt1=MyInt/10;
MyInt2=MyInt1/10;
MyInt3=MyInt2/10;
MyInt4=MyInt3/10;
if (MyInt1 < 1) {
System.out.print("The middle digit is: ");
System.out.println(MyInt);
}else if (MyInt2 < 1) {
System.out.println("No middle digit");
}else if (MyInt3 < 1) {
System.out.print("The middle digit is: ");
System.out.println(MyInt2);
}else if (MyInt4 < 1) {
System.out.println("No middle digit");
}
Upvotes: 2