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?

Recommended Answers

All 3 Replies

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

huh? Sorry I am not understanding your answer.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.