So far, this is my code its an assignment to create a number to word code
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number from 1 - 999");
int number = Integer.parseInt(in.readLine());
String[]hundreds = {"","One hundred","Two hundred","Three hundred", "Four hundred", "Five hundred", "Six hundred","Seven hundred","Eight hundred","Nine hundred"};
String[]tens = {"","ten", "tewnty", "thirty","fourty" , "fifty", "sixty", "seventy","eighty", "ninety" };
String[] ones = {"", "one", "two" , "three", "four", "five", "six", "seven", "eight", "nine", "eleven"};
int H=number/100;
int remainder= number %100;
System.out.print(hundreds[H]);
int I= remainder / 10;
int remainder1 = remainder % 10;
int J = remainder1 / 1;
int remainder2= remainder%1;
if (remainder1 <=11 || remainder1 >=19){
System.out.print(teens[J]);
}
else{
System.out.print(tens[I]);
System.out.print(ones[J]);
}
}
}
Okay, original problem fixed, where it wouldn't show up, but 561 shows up as five hundred eleven, which part is the error and how can I fix it, and what was wrong
change to
int H=number/100;
int remainder= number %100; // you want number here
System.out.println(hundreds[H]);
System.out.println();
int I= remainder / 10;
int remainder1 = remainder % 10; // you want remainder here
System.out.println(tens[I]);
System.out.println();
int J = remainder1 / 1;
int remainder2= remainder1 %1; // you want remainder1 here
System.out.println(ones[J]);
Also change tewnty -> twenty
Your code is a bit complicated. Here is how you can tackle it
int H = number/100;//hundreds digit
System.out.print(hundred[H]);
int remainder = number % 100;//the tens and ones digits
int I = remainder/10;//tens digit
int remainder1 = remainder % 10;//ones digit
if(remainder >= 11 && remainder <=19)
System.out.print(teens[remainder1]);//remainder1 - 1 considering the array starts from 11
else
{
System.out.print(tens[I]);
System.out.print(ones[remainder1]);
}
here, teens[]
is your array for the numbers from 11 to 19. the others are just the same. Also dont include eleven in your ones[]
array
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.