I am attempting to write a program that outputs the name of a number when you input a number. It works fine for one digit numbers, but I've hit a roadblock. Whenever I input a number greater than 10, it gives me an ArrayOutOfBoundsException. I'm not quite sure why, for I haven't worked with arrays too long. I have only written code to output names for numbers upto 20 till now. Here is the code:
package numericname;
import java.util.Scanner;
public class NumericName {
static String[] Ones = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
static String[] Mids = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x;
System.out.println("Enter a number");
x = in.nextInt();
if(x/10 <= 1)
ones(x);
if(x/10 > 1 && x/10 < 2)
mids(x);
}
public static void ones(int x) {
System.out.println(Ones[x]);
}
public static void mids(int x) {
x -= 11
System.out.println(Mids[x]);
}
}
I added the x - 11 to make the number equal its name in the string array, but it still doesn't work. What am I doing wrong? Thanks!