Hi I am trying to write a program that asks the user for a 2 digit number and then takes that number and prints out its english name.
ex: 45
Forty - Five

The problem I am having at the moment is that I cant figure out how to seperate the 2 numbers so I can work with them seperatly.

this is what I have tried so far:

import jpb.*;
public class NumberName {
    public static void main (String[]args){
       String Number;
       int First,Second;
        SimpleIO.prompt("Please enter a 2 digit Number:");
        Number = SimpleIO.readLine();
        //if (Number.length() > 2 || Number.length() < 1){
        //    System.out.println("Please only use 2 digit numbers");
        //}
        
        First = Integer.parseInt (Number.substring(0));
        Second = Integer.parseInt (Number.substring(1));
         System.out.println(First);
         System.out.println(Second);

    }
}

Unfortunately it prints out like this:
Please enter a 2 digit Number:25
25
5

How can I only get the first number?

input_number divide by 10: That's the first number (integer division truncates)
input_number minus 10* first_number: That's the second number

Edited 6 Years Ago by griswolf: n/a

you can use charAt on the string value if you prefer; or substring (correctly) but math works too.

Consider the number Td (T tens plus d (for digits) ones)
If you divide: Td/10 == T
If you multiply T*10 == T0, and Td-T0 == d
Now you have T and d, as requested.

Why your code gives 25 for the first digit: Look again at the docs for substring() Be sure to notice how many overloads there are for that method.

Edited 6 Years Ago by griswolf: n/a

This article has been dead for over six months. Start a new discussion instead.