0

I'am beginner in Java, and I'am having a lot of problems while typing or making the sum of numbers. The fact is that I want to calculate the sum of pressed numbers until I press -9999, meanwhile when I press -9999, I want program me to show the sum of pressed numbers.
The main problem is solved, that has to do with converting numbers to word.

import java.util.*;
 
public class NumberToWord {
public static final String[] DIGITS = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
public static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
public static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
 
// use for every grouping of 10^3 - thousands, millions, billions, etc.
public static String wordifyNumber(int number) {
StringBuilder sb = new StringBuilder();
int x = number / 100;
if (x > 0) { // something in hundreds column
sb.append(DIGITS[x - 1] + " hundred");
}
x = number % 100;
int tens = x / 10;
if (tens > 0) { // something in tens column
if (sb.length() > 0) {
sb.append(" ");
}
if (tens > 1) {
sb.append(TENS[tens - 1]);
} else {
sb.append(TEENS[x - 10]);
// TEENS accounts for tens + digit; done!
number = 0;
}
}
x = number % 10;
if (x > 0) { // something in digits column
if (sb.length() > 0) {
if (tens >= 2) { // twenty-, thirty-, etc.
sb.append("-");
} else {
sb.append(" ");
}
}
sb.append(DIGITS[x - 1]);
}
return sb.toString();
}
 
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter a number (or -1 to quit): ");
int number = scanner.nextInt();
if (number == -1) {
break;
} else if (number == 0) {
System.out.println("Number = zero");
} else {
System.out.print("Number = ");
if (number > 999) {
System.out.print(
wordifyNumber(number / 1000) + " thousand");
number = number % 1000;
System.out.print(number > 99 ? ", " : " ");
}
System.out.println(
wordifyNumber(number));
}
}
System.out.println("Done.");
}
}

Any quick help would be aprecciated.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by javinpaul
0

Use
currentNumber = //your current Number
result = resutlt + currentNumber;

while ending you can print result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.