Reputation: 27
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.
Upvotes: 2
Views: 742
Reputation: 6178
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();
}
}
Upvotes: 0
Reputation: 4471
Right now you are converting int
to String
, String
to char
s, 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
Upvotes: 1
Reputation: 820
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;
}
}
}
Upvotes: 0
Reputation: 26077
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
Upvotes: 0