0

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!

2
  • If x = 11 , it will go into the first if and one(x) will be excuted. Commented Nov 26, 2015 at 4:20
  • Oh right, because these variables are ints!! Oh, Thanks so much! Commented Nov 26, 2015 at 4:22

6 Answers 6

1

Two problems, both relating to you being a victim of Integer Division:

In Java, when you divide two integers, the result is the value of the equation with the decimal truncated. This means that the decimal part is 'cut-off'. For example: 11 / 10 = 1, 19 / 10 = 1, 9 / 10 = 0

Now for the problems:

  1. The first if statement will cause any number less than 20 to evaluate to true (because 19 / 10 == 1).

To fix this: Change it to if (x <= 10)

  1. The second if statement will never be true because x/10 cannot be less than 2 and greater than 1 (because it is an integer!)

To fix this, change it to either:

  • if(x < 20)
  • or, if(x / 10 == 1)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I try to keep that in mind always, but it completely slipped out this time!
0

I suppose there are two issues here :

Why are you trying to use multiple arrays, you can club the two. Secondly, why are using x/10, you can simply compare x<10 or x>10 && x<20.

1 Comment

You know, I really never thought of that. I keep trying to make things harder for myself. Thanks!
0

Your code is checking if (x/10 <= 1), so then the 10s will be true and drop into the ones code in this case. i.e. 11/10 == 1

1 Comment

Ah, I see, since it's an int.. Thanks!
0

my suggested edit for the main() is as follows:

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) {
      ones(x);
   } else if(x < 20) {
      mids(x);
   }
}

Comments

0

The problem lies in the fact that the division you are attempting is integer division, and this will not work as you expect. Any number from 11 to 19 will yield x/10 as 1 and then the function

ones(x)

Will lead to an ArrayIndexOutOfBounds

Comments

0

It's because, x/10 still gives you 1. It's an integer division. You have to change it to floating point.

Just change x/10 and x/20 to x/10.0 and x/20.0 everywhere. Or just cast x to float during division.

Example

if((float)x/10 <= 1)
    ones(x);
if((float)x/10 > 1 && (float)x/10 < 2)
    mids(x);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.