2

So I am trying to convert an int to string and then charAt(0), charAt(1), and charAt(2). I did that to split the 3 digit int to 3 different integers. I want to then convert those individual integers to Strings.

What I am trying to do is take numbers from 101 and above and print them in words. I have hundreds, tens and ones methods. I am trying to take first integer and apply it to the hundreds method, second integer and apply it to the tens, and third integer to ones method.

this is the method of >= 101

import java.util.Scanner;


public class rough {

    public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
        } else {
            System.out.println("please input a number from 101: ");
        }
        //this is what i have so far(might be junk).

    public static void From101(int num) {
        String SNumber = Integer.toString(num);
        char First = SNumber.charAt(0);
        char Second = SNumber.charAt(1);
        char Third = SNumber.charAt(2);
        int num1 = Integer.parseInt(first);
    }
}

Now I am trying to print the words and i am getting 3 errors.

System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));

I add that line in my if/else statement and the errors are:

 ----jGRASP exec: javac -g rough.java

rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                     ^
rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                             ^
rough.java:27: error: 'void' type not allowed here
                           System.out.println(hundred(first) + " AND" + tens(second) + "" + From1To19(third));
                                                                                     ^
3 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
9
  • 3
    This is unclear. Are you trying to convert string to int (as stated in title), int to string (as stated in post), or int to chars (as shown in code)? Commented Nov 21, 2014 at 4:17
  • @RogueBaneling well, the user inputs 3 digit number, i want to convert 3 digit number to string and split them into 3 different character. and then back to integer Commented Nov 21, 2014 at 4:18
  • What if the number doesn't have three digits? Commented Nov 21, 2014 at 4:20
  • Well you can get an array of the numbers by doing SNumber.split(""); and then you can use parseInt directly. Otherwise you'd need Integer.parseInt(String.valueOf(SNumber.charAt(0)));. Commented Nov 21, 2014 at 4:20
  • 1
    You could just use % and / - it might be easier. Commented Nov 21, 2014 at 4:22

4 Answers 4

1

Right now you are converting int to String, String to chars, and char back to int.

You can skip all of this and go directly from int -> int, using modular division.

For example, to get the individual digits of 12345:

int a = 12345;
int b = a%10; //b = 5
a = a / 10; //now a = 1234
int c = a%10; //c = 4
a = a / 10; //now a = 123
int d = a%10; //d = 3
a = a / 10; //now a = 12
int e = a%10; //e = 2
Sign up to request clarification or add additional context in comments.

7 Comments

I think, the OP is looking for this rather than num->String->Char->num
This is much more efficient! :)
You need to post your hundred() function, but I'm guessing that the function is returning void instead of something that can be converted to a String for printing.
@RogueBaneling how would i have 3 methods print on same line? if a user types 101 the output would be ONE HUNDRED AND ONE
Are you asking how to append multiple method returns into a single string to print? You are doing that properly already. Your issue is with your methods to interpret the number and generate the written form.
|
0

Does this help you.

public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            From101(number);
        } else {
            System.out.println("please input a number from 101: ");
        }

    }

    private static void From101(int num) {
        String SNumber = Integer.toString(num);
        int num1 = Character.getNumericValue(SNumber.charAt(0));
        int num2 = Character.getNumericValue(SNumber.charAt(1));
        int num3 = Character.getNumericValue(SNumber.charAt(2));
        System.out.println(num1 + " " + num2 + " " + num3);
    }

Output

Please type a number between 0 and 999 OR type -1 to exit:  101
1 0 1

Refer this

Comments

0

This will work fine for you whereas .... In this you can increase your range of numbers....

import java.util.Scanner;

public class rough {

    public static void main(String args[]) {
        int number = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
        number = scanner.nextInt();
        if (number >= 101) {
            From101(number);
        } else {
            System.out.println("please input a number from 101: ");
        }
        //this is what i have so far(might be junk).

    public static void From101(int num) {
       while(num>0)
       {
         d=num%10;
         System.out.print(d + " ");
         num=num/10;
       }
    }
}

Comments

0

This is pretty easy actually if you use the API:

public static void main(String[] args)
{
    Scanner scanner = new Scanner(System.in);
    System.out.print("Please type a number between 0 and 999 OR type -1 to exit:  ");
    int number = -1;
    try
    {
        number = scanner.nextInt();
        String numString = String.valueOf(number);
        char[] digits = numString.toCharArray();
        System.out.println(numString);
        for (char digit : digits)
        {
            System.out.println(digit);
        }
    }
    catch (InputMismatchException e)
    {
        System.err.println("Entered string is not numeric. Exiting program...");
    }
    finally
    {
        scanner.close();
    }
}

1 Comment

why the hate? There is nothing wrong with this answer. From the OP "i want to convert 3 digit number to string and split them into 3 different character. and then back to integer". This code converts an integer into a String, a String into a character array, and back to integer is not needed because the original integer is preserved. So why is the answer not appropriate? AND, there is nothing on the OP's post that states API methods are not allowed.

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.