Question: //package CheckWriter; //import com.sun.tools.javac.comp.Check; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.DecimalFormat; import java.lang.*; public class CheckWriter { //Varibles String payName; String date; double amount1;
//package CheckWriter;
//import com.sun.tools.javac.comp.Check;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.lang.*;
public class CheckWriter {
//Varibles
String payName;
String date;
double amount1;
String amount2;
private static final String[] payNames = {"", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
private static final String[] numNames = {"", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
private static String converts(int num){
String give;
if(num % 100 < 20){
give = numNames[num % 100];
num = num / 100;
}else{
give = numNames[num % 10];
num = num / 10;
give = payNames[num % 10] + give;
num = num / 10;
}//end if
if(num == 0){
return give;
}//end if
return numNames[num] + " hundred" + give;
}
public static String convert(long num){
String number = Long.toString(num);
String mask = "000000000000";
int billions = Integer.parseInt(number.substring(0, 3));
int millions = Integer.parseInt(number.substring(3, 6));
int thousands = Integer.parseInt(number.substring(9, 12));
int hundredthousands = Integer.parseInt(number.substring(6, 9));
String conBillions;
if(num == 0){
return "zero";
}
DecimalFormat df = new DecimalFormat(mask);
number = df.format(num);
switch(billions){
case 0:
conBillions = "";
break;
case 1:
conBillions = converts(billions) + " billion ";
break;
default:
conBillions = converts(billions) + " billion ";
break;
}//end
String result = conBillions;
String conMillions;
switch(millions){
case 0:
conMillions = "";
break;
case 1:
conMillions = converts(millions) + " millions ";
break;
case 2:
conMillions = converts(millions) + " millions ";
break;
default:
conMillions = converts(millions) + " millions";
break;
}//end
result += conMillions;
String conHundredThousands;
switch(hundredthousands){
case 0:
conHundredThousands = "";
break;
case 1:
conHundredThousands = "one thousand ";
break;
default:
conHundredThousands = converts(hundredthousands) + " thousand ";
}//end
result += conHundredThousands;
String conThousands;
conThousands = converts(thousands);
result += conThousands;
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}//end method
private void displayString(){
System.out.println("--------------------------------------------------------------------------");
System.out.println(" Date: " + date);
System.out.println(" Pay to the order of: " + payName + "\t\t\t\t $" + amount1);
System.out.println(" \t\t\t\t\t\t\t");
System.out.println(" " + amount2);
System.out.println(" \t\t\t\t\t\t\t");
System.out.println("--------------------------------------------------------------------------");
}//end
private void convertString(){
DecimalFormat df = new DecimalFormat("0.##");
String str = Double.toString((amount1));
int length = str.length();
long dollars = Long.parseLong(str.substring(0, length-3));
int cents = Integer.parseInt(str.substring(length-2, length));
//output
System.out.println(cents);
amount2 = convert(dollars) + " and " + converts(cents) + " cents."; //The error is here, which I am assuming that the method convert could be wrong.
}//end
public static void main(String[] args)throws IOException{
CheckWriter cw = new CheckWriter();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//boolean correctDate = false;
try{
System.out.print("Enter the date as mm/dd/yyyy: ");
cw.date = input.readLine();
System.out.print("Enter the payee's name: ");
cw.payName = input.readLine();
System.out.print("Enter the amount of the check: ");
cw.amount1 = Double.parseDouble(input.readLine());
cw.convertString();
cw.displayString();
}catch (IOException ioe){
}//end try and catch
}//end main method
}
I have an error and I don't know where I am not able to see the error. Can you please help me here. Thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
