I'm new to programming and I've just started learning Java.
I want to do a program that's
- asks the user to enter a string that contains a sequence of numbers and then
- takes the first and the last numbers of that sequence and
- check if these numbers are an odd or even
Based on that information it will do certain things.
Here's my code:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String n = kb.nextLine();
Integer x = Integer.valueOf(n.charAt(n.length() - 1));
Integer y = Integer.valueOf(n.charAt(0));
String out;
if (y % 2 == 0 && x % 2 == 0) {
out = "$" + n.substring(1, n.length() - 1) + "$";
} else if (y % 2 > 0 && x % 2 > 0) {
out = "X" + n.substring(1, n.length() - 1) + "X";
} else if (x == 0); {
out = n.substring(0, n.length() - 1) + "#";
}
System.out.println(out);
}
I'm not sure what's the problem. I think its about those two lines
Integer x = Integer.valueOf(n.charAt(n.length()-1));
Integer y = Integer.valueOf(n.charAt(0));
The output value is different than the one in the input..