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.
schleepy
  • 27
  • 1
  • 7
  • 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)? – RogueBaneling Nov 21 '14 at 04: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 – schleepy Nov 21 '14 at 04:18
  • What if the number doesn't have three digits? – MadProgrammer Nov 21 '14 at 04: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)));`. – ChiefTwoPencils Nov 21 '14 at 04:20
  • `Integer.parseInt` expects a `String`, you need to convert `char` to a `String`. Once that's done (and discarding numbers of less than 3 digits), works okay for me... – MadProgrammer Nov 21 '14 at 04:21
  • @MadProgrammer I have other methods that will output the word just fine, its just numbers above 101 to 999 – schleepy Nov 21 '14 at 04:21
  • 1
    You could just use `%` and `/` - it might be easier. – Dawood ibn Kareem Nov 21 '14 at 04:22
  • yup, modular division serves his purpose. good thinking David – spiderman Nov 21 '14 at 04:28
  • Java is case sensitive, `First` is not the same as `first`...You might like to have a read through [Code Conventions for the Java TM Programming Language](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html), it will make it easier for people to read your code and for you to read others – MadProgrammer Nov 21 '14 at 04:45

4 Answers4

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
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • I think, the OP is looking for this rather than num->String->Char->num – spiderman Nov 21 '14 at 04:26
  • This is much more efficient! :) – schleepy Nov 21 '14 at 04:28
  • 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 Nov 21 '14 at 04:43
  • @RogueBaneling how would i have 3 methods print on same line? if a user types 101 the output would be ONE HUNDRED AND ONE – schleepy Nov 21 '14 at 05:12
  • 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. – RogueBaneling Nov 21 '14 at 05:15
  • @RogueBaneling Could you help me with it? – schleepy Nov 21 '14 at 05:28
  • You should create a new question for that, including any ideas you have on how you could convert an integer to a word, and any existing code you have that could be used to solve the problem. – RogueBaneling Nov 21 '14 at 05:32
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

Community
  • 1
  • 1
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
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;
       }
    }
}
Mudit Saklani
  • 752
  • 4
  • 10
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();
    }
}
hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • 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. – hfontanez Nov 21 '14 at 04:34