How to make one class into two classes - java

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;
}
}

Related

Converting Integers to Roman Numerals in Java

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;
}

Modify the program to accept a telephone number with any number of letters.

Modify the program to accept a telephone number with any number of letters. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
Below is what i have to edit.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
}
This part is what i tried.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
while(telephone_letter !="2")
{
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
}
My output is wrong.
Enter Telephone letters : fewfwef
======================================
The Telephone letter is : fewfwef
The Phone number is : 339-3933
=======================================
Enter Telephone letters : wsqsq
=======================================
The Telephone letter is : wsqsq
The Phone number is : 339-3933
=======================================
And another Question is how do i do this The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits.
This seems like a homework problem so here are some hints to help you solve it yourself.
(1) Using a fixed number like 100 is rarely a good idea:
int[] number = new int[aChar.length];
(2) The scanner can tell you if there is more to be scanned(read):
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
while (keyboard.hasNextLine()) {
String telephone_letter = keyboard.nextLine();
...
System.out.println("");
System.out.print("Enter Telephone letters : ");
}
(3) A loop is good for counting. If the first count is different, then start different:
int todash = 3;
System.out.print("The Phone number is : ");
for (int i=0; i < number.length; ++i) {
if (todash == 0) {
System.out.print("-");
todash = 4;
}
System.out.print(number[i]);
--todash;
}
System.out.println("");
I've rewritten you're code. It may be easier to work with.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
String input = keyboard.nextLine();
TelephoneProcessor telephoneProcessor = new TelephoneProcessor(input);
String result = telephoneProcessor.proccess();
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + input);
System.out.println("The Phone number is : " + result);
System.out.println("=======================================");
}
}
class TelephoneProcessor {
Map<String, String> matches = new HashMap<>();
String toProcess;
public TelephoneProcessor(String toProcess) {
this.toProcess = toProcess;
matches.put("[a-c]", "2");
matches.put("[d-f]", "3");
matches.put("[g-i]", "4");
matches.put("[j-l]", "5");
matches.put("[m-o]", "6");
matches.put("[p-r]", "7");
matches.put("[s-w]", "8");
matches.put("[x-z]", "9");
}
public String proccess() {
if(toProcess != "2") {
matches.forEach((regex, replacement) -> {
toProcess = toProcess.replaceAll(regex, replacement);
});
}
return toProcess.substring(0,3) + "-" + toProcess.substring(3,7);
}
}
It only asks once but that's easy to solve by making a class that contains the main method in a method and loops it.
I hope this helps for further extensions of the program :)

Input Repeat with same case

After I run this codes, it says BUILD SUCCESSFUL and it stops. What should I do to make it repeat to ask another input with same case? I did read tutorial on several web but I'm not helped by those.
Here's the code:
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
I appreciate any help that you can provide.
I would try using a while loop to encase the switch. So it keeps asking your input.
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
while(true){
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
}
As suggested by MadProgarmmer do-while loop is the perfect use-case for such scenarios. You need to wrap your input line plus switch cases and print statement inside the do-while loop as follow:
do {
//so you know that terminal is asking for input
System.out.println("Input: ");
mth =scanner.nextInt();
//copy/paste your switch case with last System.out.println() here
} while (true);
I am worried about having execution that never ends. Maybe you want to end the loop when the user supplies -1, so change the while(true) to while (mth != -1) and that should be your termination input.

Convertion of numeros

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)) {
...
}
}

Filling in array from end

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':
}
}

Categories

Resources