The homework for my Java class instructs us to take an integer input from the user (up to 3999) and convert it into a Roman Numeral. The trouble I am having with the code is that when I input the integer while testing, it sets the integer to 0 and the Roman Numeral prints out as null. This could be a result of declaring the variables inside an object but im unsure at this point.
public class RomanNumerals
{
// instance variables
int input;
String romanNum;
/**
* Object that records the input from a user.
*/
public RomanNumerals()
{
int input=0;
String romanNum="";
//System.out.println("Input a number to get a Roman Numeral");
//int input = integer.nextInt();
}
public int Scanner()
{
Scanner integer = new Scanner(System.in);
System.out.println("Input a number to get a Roman Numeral: ");
int input = integer.nextInt();
return input;
}
/**
* Object that takes the input from the user and determines what roman numerals are appropriate.
*/
public String Int2Roman()
{
if (input < 1 || input > 3999)
{
System.out.println("Please input a number between 1 and 3999.");
}
while (input >= 1000)
{
romanNum = romanNum + "M";
input = input - 1000;
}
while (input >= 900)
{
romanNum = romanNum + "CM";
input = input - 900;
}
while (input >= 500)
{
romanNum = romanNum + "S";
input = input - 500;
}
while (input >= 400)
{
romanNum = romanNum + "CS";
input = input - 1000;
}
while (input >= 100)
{
romanNum = romanNum + "C";
input = input - 100;
}
while (input >= 90)
{
romanNum = romanNum + "XC";
input = input - 1000;
}
while (input >= 50)
{
romanNum = romanNum + "L";
input = input - 1000;
}
while (input >= 40)
{
romanNum = romanNum + "XL";
input = input - 40;
}
while (input >= 10)
{
romanNum = romanNum + "X";
input = input - 10;
}
while (input >= 9)
{
romanNum = romanNum + "IX";
input = input - 9;
}
while (input >= 5)
{
romanNum = romanNum + "V";
input = input - 5;
}
while (input >= 4)
{
romanNum = romanNum + "IV";
input = input - 4;
}
while (input >= 1)
{
romanNum = romanNum + "I";
input = input - 5;
}
System.out.println("The Roman Numeral of " + input + " is " + romanNum);
return romanNum;
}
public static void main(String[] args)
{
RomanNumerals a = new RomanNumerals();
a.Scanner();
a.Int2Roman();
}
}
Your Scanner method does not store the keyboard input in the field (instance variable) input but in a local variable input.
Replace
int input = integer.nextInt(); //this declares a new local variable
with
input = integer.nextInt(); //uses the existing field
try this to see if works , works for me, simple , basic , pretty straight forward
public static void main(String[] args) {
//INPUNT INTEGER VALUE
// OUTPUT STRING VALUE (SYSTEM CONSOLE OUTPUT
//**********************************************************************************//
int _inValue = 117; //int parameter value
//**********************************************************************************//
//check if the number is between 1 and 3999
if (_inValue < 1){
System.out.println("integer can't be less than 1");
return;
}
if (_inValue > 3999){
System.out.println("integer can't be more than 3999");
return;
}
//convert the value in array to work by separate numbers
char[] arg = String.valueOf(_inValue).toCharArray();
//count the number of characters to use a case condition or length of the integer in value
int _count = Integer.toString(_inValue).length();
// output variable
String _output = "";
//case condition base in the length of the integer value
switch (_count){
case 1: // case base in one figure (1)
switch (_inValue){
case 1:
_output = "I";
break;
case 2:
_output = "II";
break;
case 3:
_output = "III";
break;
case 4:
_output = "IV";
break;
case 5:
_output = "V";
break;
case 6:
_output = "VI";
break;
case 7:
_output = "VII";
break;
case 8:
_output = "VIII";
break;
case 9:
_output = "IX";
break;
}
break;
case 2: //case base in two figures (10)
switch (Character.getNumericValue(arg[0])){//base in the first character of the integer value lets use another case condition
case 1:
_output = "X";
break;
case 2:
_output = "XX";
break;
case 3:
_output = "XXX";
break;
case 4:
_output = "XL";
break;
case 5:
_output = "L";
break;
case 6:
_output = "LX";
break;
case 7:
_output = "LXX";
break;
case 8:
_output = "LXXX";
break;
case 9:
_output = "XC";
break;
}
_output += _getValue10(Character.getNumericValue(arg[1])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
break;
case 3: //case base in three figures (100)
switch (Character.getNumericValue(arg[0])){ //base in the first character of the integer value lets use another case condition
case 1:
_output = "C";
break;
case 2:
_output = "CC";
break;
case 3:
_output = "CCC";
break;
case 4:
_output = "CD";
break;
case 5:
_output = "D";
break;
case 6:
_output = "DC";
break;
case 7:
_output = "DCC";
break;
case 8:
_output = "DCCC";
break;
case 9:
_output = "CM";
break;
}
_output += _getValue100(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
break;
case 4: // case base in 4 figures (1000)
if(_inValue != 1000){ // validate the value if its different of a 1000
// use a cycle to concat the string base of the number of the integer in value
for (int i = 0; i < Character.getNumericValue(arg[0]);i++ ){
_output += "M";
}
}else{
// if its a 1000 use this one and that's it
_output += "M";
}
_output += _getValue1000(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2]),Character.getNumericValue(arg[3])); //lets call this function to get the other three figures or number to complete the integer amount and build the roman value in string
break;
}
System.out.println("the integer number is: " + _inValue);
System.out.println("the roman number is: " + _output);
}
///method who return the other three values
private static String _getValue1000(int valueOne, int valueTwo, int valueThree){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "C";
break;
case 2:
_output += "CC";
break;
case 3:
_output += "CCC";
break;
case 4:
_output += "CD";
break;
case 5:
_output += "D";
break;
case 6:
_output += "DC";
break;
case 7:
_output += "DCC";
break;
case 8:
_output += "DCCC";
break;
case 9:
_output += "CM";
break;
}
}
if(valueTwo != 0){
switch (valueTwo){
case 1:
_output += "X";
break;
case 2:
_output += "XX";
break;
case 3:
_output += "XXX";
break;
case 4:
_output += "XL";
break;
case 5:
_output += "L";
break;
case 6:
_output += "LX";
break;
case 7:
_output += "LXX";
break;
case 8:
_output += "LXXX";
break;
case 9:
_output += "XC";
break;
}
}
if(valueThree != 0){
switch (valueThree){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
/// method who return the other two values
private static String _getValue100(int valueOne, int valueTwo){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "X";
break;
case 2:
_output += "XX";
break;
case 3:
_output += "XXX";
break;
case 4:
_output += "XL";
break;
case 5:
_output += "L";
break;
case 6:
_output += "LX";
break;
case 7:
_output += "LXX";
break;
case 8:
_output += "LXXX";
break;
case 9:
_output += "XC";
break;
}
}
if(valueTwo != 0){
switch (valueTwo){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
/// method who return one other value
private static String _getValue10(int valueOne){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
Related
I have an assignment but i couldn't complete this question.
Previously I add inserted else if (counter == 7) { break; } after the counter==3 but I had removed since the question said to process as many digits as the user wants.
I do not know how to add to count every 4 digit for dash after the first 3 digits dash.
the code below is what I have.
please help and explain to me if possible. thanks :)
import java.util.Scanner;
public class question1 {
public static void main (String [] args)
{
boolean notletter = false;
Scanner console = new Scanner(System.in);
String phoneletter = "";
int counter = 0;
System.out.print("Enter a phone number in letters only: ");
String phoneNumber = console.nextLine();
String phnum2 = phoneNumber.replaceAll(" ", "");
for (int i = 0; i <= phnum2.length() - 1; i++) {
if (!(Character.isLetter(phnum2.charAt(i)))) {
notletter = true;
break;
}
{
switch (Character.toUpperCase(phnum2.charAt(i))) {
case 'A':
case 'B':
case 'C':
phoneletter = phoneletter + "2";
break;
case 'D':
case 'E':
case 'F':
phoneletter = phoneletter + "3";
break;
case 'G':
case 'H':
case 'I':
phoneletter = phoneletter + "4";
break;
case 'J':
case 'K':
case 'L':
phoneletter = phoneletter + "5";
break;
case 'M':
case 'N':
case 'O':
phoneletter = phoneletter + "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
phoneletter = phoneletter + "7";
break;
case 'T':
case 'U':
case 'V':
phoneletter = phoneletter + "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
phoneletter = phoneletter + "9";
break;
}
counter++;
if (counter ==3) {
phoneletter = phoneletter + "-";
}
}
}
if (notletter == true) {
System.out.println("Please indicate only alphabets");
} else {
System.out.println(phoneletter);
}
}
}
This can be done by editing the if-statement at the end of your code:
if (counter ==3) {
phoneletter = phoneletter + "-";
}
Here you already checked if the counter is three. If it is, add a hyphen. The next thing to do is to check the case of "a hyphen after every 4 digits". To do this, we can check if counter - 3 is a multiple of 4:
(counter - 3) % 4 == 0
However, this would make it insert an extra - at the end of the output if (input length - 1) is a multiple of 4. So we shouldn't add a hyphen if we reached the end of the string. This can be done by checking whether i is phnom.length() - 1:
i != phnom.length() - 1
If we combine all these conditions together, we get
if (counter == 3 || ((counter - 3) % 4 == 0 && i != phnom.length() - 1)) {
Note: This can actually be done using a regular expression, but since this is homework, I doubt this approach will be accepted. Here is how you do this with regex:
// phoneLetter is a string of digits without any hyphens
String result = phoneLetter.replaceAll("^\\d{3}|\\d{4}(?!$)", "$0-");
I finished a program which is to make a number between 1 to 3999 and change it to roman numerals and I got it to work but I have to put this in two classes a main class and a tester class how would I do this? I know this may be a really simple question but I can't seem to figure it out how to split them into a main class and a tester class.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
String numberString="";
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
System.out.println(number + " in Roman numerals is " + numberString);
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
As #Vikrant Kashyap pointed out. You can break this down to the test class and the conversion class. I didn't have a chance to compile the code. Let me know if this works or not.
RomanNumeralsTest.java
public class RomanNumeralsTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
RomanNumerals rn = new RomanNumerals();
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
System.out.println(number + " in Roman numerals is " + rn.convertToRomanNumeral(number));
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
}
RomanNumerals.java
public class RomanNumerals
{
public String convertToRomanNumeral(int number)
{
String numberString = "";
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
return numberString;
}
}
I just saw some of my work today, and i want to enter the user how many alphabet they want and continue converting. Can i have help? It would be fantastic. I tried anything. I'm just new in java programming that's why. Thanks :)
This is my whole programming
import java.util.Scanner;
public class Try2 {
public static void main(String[] args) {
int counter = 0;
int it = 0;
Scanner keyboard = new Scanner(System. in );
System.out.println("Enter words to digits: ");
String alpha = keyboard.nextLine();
alpha = alpha.toLowerCase();
String num = (" ");
while (counter < alpha.length()) {
switch (alpha.charAt(it)) {
case 'A':
case 'a':
case 'B':
case 'b':
case 'c':
case 'C':
num += "2";
counter++;
break;
case 'D':
case 'E':
case 'F':
case 'd':
case 'e':
case 'f':
num += "3";
counter++;
break;
case 'G':
case 'H':
case 'I':
case 'g':
case 'h':
case 'i':
num += "4";
counter++;
break;
case 'J':
case 'K':
case 'L':
case 'j':
case 'k':
case 'l':
num += "5";
counter++;
break;
case 'M':
case 'N':
case 'O':
case 'm':
case 'n':
case 'o':
num += "6";
counter++;
break;
case 'P':
case 'R':
case 'S':
case 'p':
case 'r':
case 's':
num += "7";
counter++;
break;
case 'T':
case 'U':
case 'V':
case 't':
case 'u':
case 'v':
num += "8";
counter++;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case ' ':
num += "9";
counter++;
break;
}
if ((counter % 4) == 3) {
num += "-";
}
it++;
}
System.out.println(num);
}
}
You can wrap the read line code with another while loop:
Scanner keyboard = new Scanner(System.in);
while(true) {
System.out.println("Enter words to digits: ");
String alpha = keyboard.nextLine();
alpha = alpha.toLowerCase();
String num = " ";
int counter = 0;
int it = 0;
while (counter < alpha.length()) {
...
}
//new line (optional)
num += "\n";
}
You can check if the user entered a flag to end input:
//You can choose any flag you want
if(alpha.equals("finish")) {
break;
}
About your switch:
You can remove all the upper case letters because you wrote: alpha = alpha.toLowerCase();
You forgot 'q'
If alpha don't match: [a-zA-z ] (contains only letters and spaces) an IndexOutOfBoundsException will be thrown from alpha.charAt(it) because you don't increase counter and the loop don't exit in time.
To prevent the last problem you can remove counter and it and change the while loop to for loop:
for (int i = 0; i < alpha.length(); i++) {
if ((i % 4) == 3) {
num += "-";
}
switch (alpha.charAt(i)) {
...
}
}
I am currently working on a two way morse code translator program. I am not quite finished but when doing a test of the program I received multiple compilation errors. I have no idea what is wrong but am sure it is something simple. Any help would be appreciated.
Here is my code so far:
public class MorseCodeProject
{
public static void main(String[] args)
{
System.out.println("Please enter the conversion to be preformed.");
System.out.println("Type 1 to convert English to Morse code.");
System.out.println("Type 2 to convert Morse code to English.");
int type = Input.getInt();
System.out.println("Please enter the message to be converted.");
String message = Input.getString();
if(type == 1)
EtoM(message);
else
MtoE(message);
}
public static void EtoM(String message)
{
String translation[] = new String[message.length()];
String y;
for(int x = 0; x < message.length(); x++)
{
y = message.substring(x, x + 1);
switch(y)
{
case "a":
translation[x] = ".-";
break;
case "b":
translation[x] = "-...";
break;
case "c":
translation[x] = "-.-.";
break;
case "d":
translation[x] = "-..";
break;
case "e":
translation[x] = ".";
break;
case "f":
translation[x] = "..-.";
break;
case "g":
translation[x] = "--.";
break;
case "h":
translation[x] = "....";
break;
case "i":
translation[x] = "..";
break;
case "j":
translation[x] = ".---";
break;
case "k":
translation[x] = "-.-";
break;
case "l":
translation[x] = ".-..";
break;
case "m":
translation[x] = "--";
break;
case "n":
translation[x] = "-.";
break;
case "o":
translation[x] = "---";
break;
case "p":
translation[x] = ".--.";
break;
case "q":
translation[x] = "--.-";
break;
case "r":
translation[x] = ".-.";
break;
case "s":
translation[x] = "...";
break;
case "t":
translation[x] = "-";
break;
case "u":
translation[x] = "..-";
break;
case "v":
translation[x] = "...-";
break;
case "w":
translation[x] = ".--";
break;
case "x":
translation[x] = "-..-";
break;
case "y":
translation[x] = "-.--";
break;
case "z":
translation[x] = "--..";
break;
case "1":
translation[x] = ".----";
break;
case "2":
translation[x] = "..---";
break;
case "3":
translation[x] = "...--";
break;
case "4":
translation[x] = "....-";
break;
case "5":
translation[x] = ".....";
break;
case "6":
translation[x] = "-....";
break;
case "7":
translation[x] = "--...";
break;
case "8":
translation[x] = "---..";
break;
case "9":
translation[x] = "----.";
break;
case "0":
translation[x] = "-----";
break;
case " ":
translation[x] = "|";
default:
break;
}
for(int z = 0; z < message.length(); z++)
{
System.out.println(translation[z]);
}
}
}
public static void MtoE(String message)
{
int segments = 1;
char segment1;
int x = 0;
for(x = 0; x < message.length(); x++);
{
if(message.charAt(x) = "|")
{
segments += 1;
}
}
String segmentedMessage[] = new String[segments];
int j = 0;
int k;
for(int y = 0; y < segments; y++)
{
for(k = j; k < message.length(); k++)
{
if(message.charAt(k) == "|")
{
segementedMessage[y] = message.substirng(j, k);
j = k;
}
}
}
String segment;
String translation[] = new String[segments]
for(int c = 0; c < segments; c++)
{
segment = segmentedMessage[c];
switch(segment)
{
case ".-":
translation[c] = "a";
break;
case "-...":
translation[c] = "b";
break;
case "-.-.":
translation[c] = "c";
break;
case "-..":
translation[c] = "d";
break;
case ".":
translation[c] = "e";
break;
case "..-.":
translation[c] = "f";
break;
case "--.":
translation[c] = "g";
break;
case "....":
translation[c] = "h";
break;
case "..":
translation[c] = "i";
break;
case ".---":
translation[c] = "j";
break;
case "-.-":
translation[c] = "k";
break;
case ".-..":
translation[c] = "l";
break;
case "--":
translation[c] = "m";
break;
case "-.":
translation[c] = "n";
break;
case "---":
translation[c] = "o";
break;
case ".--.":
translation[c] = "p";
break;
case "--.-":
translation[c] = "q";
break;
case ".-.":
translation[c] = "r";
break;
case "...":
translation[c] = "s";
break;
case "-":
translation[c] = "t";
break;
case "..-":
translation[c] = "u";
break;
case "...-":
translation[c] = "v";
break;
case ".--":
translation[c] = "w";
break;
case "-..-":
translation[c] = "x";
break;
case "y-.--:
translation[c] = "y";
break;
case "--..":
translation[c] = "z";
break;
case ".----":
translation[c] = ".----";
break;
case "..--":
translation[c] = "..---";
break;
case "...--":
translation[c] = "...--";
break;
case "....-":
translation[c] = "....-";
break;
case ".....":
translation[c] = ".....";
break;
case "-....":
translation[c] = "-....";
break;
case "--...":
translation[c] = "--...";
break;
case "---..":
translation[c] = "---..";
break;
case "----.":
translation[c] = "----.";
break;
case "-----":
translation[c] = "-----";
break;
case "|":
translation[c] = " ";
default:
break;
}
}
}
}
Here are the problems I found:
1.
if(message.charAt(x) = "|")
should likely be
if(message.charAt(x) == '|')
The same goes for if(message.charAt(k) = "|"). Recall that = is assignment whereas == is comparison, and that charAt() returns a char as opposed to a String, so we compare with '|' and not "|" (you should use .equals() for strings anyway).
2.
segementedMessage[y] = message.substirng(j, k);
substring is mispselled here, and so is segmentedMessage (which you declare with the correct spelling earlier).
3.
String translation[] = new String[segments]
You're missing a ; at the end of this line.
4.
case "y-.--:
You're missing a closing " at the end of that string (should be case "y-.--":).
I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}