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