public static Double valueOf(String s)
throws NumberFormatException

Returns a Double object holding the double value represented by the argument string s.

Doc

But I'm not sure whether that's relevant or important...

Sorry, I missed in your original where you said what class valueOf() was in?
Since the OP was using the String version, I assumed you were also.

You're right. I shouldn't have assumed the class was Double since she scans input as a string. But I'm an idiot. ;)

I still can't get it to go after playing around with it. When I try to run the program, it gives the date, payee name, and dollar amount ($15.75) correctly, but where I'm trying to get it to write out the word version, it just prints 15.75. Here's the code I have at this point:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;
import java.text.NumberFormat;

public class checkWriter

{	// Begin public class checkWriter
	public static void main(String[] args)
	{	// Begin public static void main(String[] args)
		String input = "";			// To hold user's input
		String date = "";			// To hold the date
		String name = "";			// To hold the payee name
		String sAmount = "";		// To hold spelled out amount of check
		double amount = 0.00;		// To hold amount of check

		//Create a Scanner object to read input
		Scanner keyboard = new Scanner(System.in);

		// Get date from user
		System.out.print("Please enter the date: ");
		date = keyboard.nextLine();

		// Create StringTokenizer object
		StringTokenizer strTokenizer = new StringTokenizer(date, "/");

		// Test the date

		// Get the payee name from user
		System.out.print("Please enter the payee name: ");
		name = keyboard.nextLine();

		// Test the payee name

		// Get amount of check from user
		System.out.print("Please enter amount of check: ");
		amount = keyboard.nextDouble();

		// Test the amount of the check

		// Append values to the object
		StringBuilder str = new StringBuilder();

		str.append("Date: " +date+ "\n");					// Append date
		str.append("Pay to the Order of: " +name+ "\n");	// Append payee
		str.append("$" +amount+ "\n");						// Append amount
		str.append(sAmount+ "\n");							// Append word amount

		// Display output of check
		System.out.println(str);
		NumberFormat nf = NumberFormat.getCurrencyInstance();
    	        System.out.println(nf.format(amount));

		// Convert amount of check to spelled out amount
		//Double.parseDouble(amount);
		//System.out.println(String.valueOf(amount));

		// Exit system
		System.exit(0);

	}	// End public static void main(String[] args)

}	// End public class checkWriter
Member Avatar for coil

You don't assign sAmout to anything, so it stays empty.

Even when I try to change that, it doesn't pull the dollar amount written in words. Here's what the program does when I test it:

User enters:
11/11/2010
Joe Jones
15.00

System prints:
Date: 11/11/2010
Pay to the Order of: Joe Jones
$15.00

15.00 (THIS IS WHERE THE DOLLAR AMOUNT SHOULD BE SPELLED OUT)

Member Avatar for coil

What are you assigning to the variable that holds the word representation of the amount?

System.out.println(String.valueOf(amount));
By the way, just making sure, String.valueOf does not return a word representation, it merely converts an int to a String.

So if you have an int that contains "50", and you call String.valueOf, it returns a String with "50", not a String with "fifty".

I guess what I'm trying to use is the "sAmount" to hold the string value of the actual dollar amount that the user enters in. However, when I try to pass this in in place of "amount", I get an error stating "Cannot format given Object as a Number". I am thinking that since the program returns the date, payee name, and "amount" that this should return the written amount too. I'm very confused!

Member Avatar for coil

Well the program returns what you code. In your program, I don't see any code that converts numbers->words, and there is no such built-in function, so you're going to have to code it manually.

You can extract each digit of an int with %10, and get rid of the right-most digit with /10. Do that to get all the digits, then write a comprehensive switch-case to convert numbers to strings.

I posted a link to a tutorial that should get you more than started on writing a class that parses and converts numerical data into written English, it's on like page 2 or 3 of this thread. Also, Norm, Coil, I, and others have posted on how valueOf() does not return a written word value... it returns a string equivalent (or a Int/Double object depending on what class you import it from).

I just want to kindly remind you to carefully read the posts as many of your questions have already been answered. I would start by taking a look at the link I posted, and also think about the ways coil and Norm have suggested you implement a solution. You still have a lot of code to write left... don't give up :)

just to clear up your code a little... the NumberFormat.getCurrencyInstance() takes a number, puts a $ in front of it, and formats the decimal place holder to 2 places in front of the hundredths place (or something similar...), it doesn't return a 'written english word' representation... I had thought early you wanted it to format it that way until later posts where you clarified for me you want it written out. Also, if you wanted to implement it that way for a decimal amount, you have it in a funny place...
I put it up here...

String input="";// To hold user's input
	String date="";// To hold the date
	String name="";// To hold the payee name
	double amount = 0.01;// To hold amount of check

	//Create a Scanner object to read input
	Scanner keyboard = new Scanner(System.in);

	// Get date from user
	System.out.print("Please enter the date: ");
	date = keyboard.nextLine();

	//create Currency formater
	NumberFormat nf = NumberFormat.getCurrencyInstance();

//... skipping to the important part...

str.append("Date: "+date+"\n");// Append date
	str.append("Pay to the Order of: "+name+"\n");// Append payee
	str.append(nf.format(amount)+"\n");// Append amount
	str.append(" ");// Append word amount

	// Display output of check
	System.out.println(str);

Unless the way you wrote it is using the getCurrencyInstance() ok for you... I didn't run what you have written, just what I did yesterday. Again... you still have quite a bit of coding to do left.

I did look at the posts, but when I tried to play around with it in my code, I couldn't get it to do anything. I did get a little bit further this morning. If I enter in "15" for the dollar amount, it will return the word "fifteendollars". However, if I try to enter "15.00" or "15.75", it gives me a MismatchException error. What am I still doing wrong?

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.*;
import java.text.NumberFormat;

public class checkWriter
{	// Begin public class checkWriter

        String string;

        // Create an array to hold word values for single digits
        String stS[] = {"", "one", "two", "three", "four", "five", "six", "seven",
                        "eight", "nine"};

        // Create an array to hold word values for hundreds and thousands digits
        String stHT[] = {"hundred", "thousand"};

        // Create an array to hold word values for double digits
        String stDD[] = {"ten", "eleven", "twelve", "thirteen", "fourteen",
                        "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};

        // Create an array to hold twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety
        String stL[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy",
                        "eighty", "ninty"};


        public String convert(int number)
        {	// Begin public String convert(int number)

                int n = 1;		// Setting integer to 1 as default
                int word;		// Creating word to use in conversions
                string = "";	// String to hold user's input

                while (number != 0)
                {	// Begin while loop

                        switch (n)
                        {	// Begin switch statement
                        case 1:	// Executes if number is greater than 0
                                word = number % 100;
                                pass(word);
                                if (number > 100 && number % 100 != 0)
                                {	// Begin if statement
                                        show("and ");
                                }	// End if statement
                                number /= 100;
                                break;	// Exit if criteria in case 1 is met

                        case 2:	// Executes if number is in hundreds or thousands
                                word = number % 10;
                                if (word != 0)
                                {	// Begin if statement
                                        show(" ");
                                        show(stHT[0]);
                                        show(" ");
                                        pass(word);
                                }	// End if statement
                                number /= 10;
                                break;	// Exit if criteria in case 2 is met

                        case 3:	// Executes if number is in hundreds or thousands
                                word = number % 100;
                                if (word != 0)
                                {	// Begin if statement
                                        show(" ");
                                        show(stHT[1]);
                                        show(" ");
                                        pass(word);
                                }	// End if statement
                                number /= 100;
                                break;	// Exit if criteria in case 3 is met

                        case 4:	// Executes if number is in hundreds or thousands
                                word = number % 100;
                                if (word != 0)
                                {	// Begin if statement
                                        show(" ");
                                        show(stHT[2]);
                                        show(" ");
                                        pass(word);
                                }	// End if statement
                                number /= 100;
                                break;	// Exit if criteria in case 4 is met

                        case 5:	// Executes if number is in hundreds or thousands
                                word = number % 100;
                                if (word != 0)
                                {	// Begin if statement
                                        show(" ");
                                        show(stHT[3]);
                                        show(" ");
                                        pass(word);
                                }	// End if statement
                                number /= 100;
                                break;	// Exit if criteria in case 5 is met

                        }	// End switch statement

                }	// End while loop

                return string;

        }	// End public String convert(int number)

        public void pass(int number)
        {	// Begin public void pass(int number)

                int word, q;

                if (number < 10)
                {	// Begin if statement
                        show(stS[number]);
                }	// End if statement
                if (number > 9 && number < 20)
                {	// Begin if statement
                        show(stDD[number - 10]);
                }	// End if statement
                if (number > 19)
                {	// Begin if statement
                        word = number % 10;
                        if (word == 0)
                        {	// Begin if statement
                                q = number / 10;
                                show(stL[q - 2]);
                        }	// End if statement
                        else
                        {	// Begin else statement
                                q = number / 10;
                                show(stS[word]);
                                show(" ");
                                show(stL[q - 2]);
                        }	// End else statement
                }	// End if statement

        }	// End public void pass(int number)

        public void show(String s)
        {	// Begin public void show(String s)

                String st;
                st = string;
                string = s;
                string += st;

        }	// End public void show(String s)

        public static void main(String[] args)
        {	// Begin public static void main(String[] args)

					String input = "";			// To hold user's input
					String date = "";			// To hold the date
					String name = "";			// To hold the payee name
					int amount = 0;				// To hold amount of check

                	Num w = new Num();	// To hold written value of number

                	//Create a Scanner object to read input
                	Scanner keyboard = new Scanner(System.in);

                	// Get date from user
					System.out.print("Please enter the date: ");
					date = keyboard.nextLine();

					// Create StringTokenizer object
					StringTokenizer strTokenizer = new StringTokenizer(date, "/");

					// Test the date

					// Get the payee name from user
					System.out.print("Please enter the payee name: ");
					name = keyboard.nextLine();

					// Test the payee name

					// Get amount of check from user
					System.out.print("Please enter amount of check: ");
					amount = keyboard.nextInt();

                	// Convert dollar amount to word amount
                	String inwords = w.convert(amount);

                	// Create StringBuilder and append strings
					StringBuilder str = new StringBuilder();

					str.append("Date: " +date+ "\n");					// Append date
					str.append("Pay to the Order of: " +name+ "\n");	// Append payee
					str.append("$" +amount+ "\n");						// Append amount

					// Display output of check
					System.out.println(str);
                	System.out.println(inwords+ "dollars");

        // Exit system
		System.exit(0);

        }	// End public static void main(String[] args)

}	// End public class checkWriter

it gives me a MismatchException error

What statement gives the error?
Have you read the API doc for that error? Look at what you entered for input and how you tried to read it and think about the error message explanation.

When I try to enter in "15.00" or "15.75"- anything with a decimal point, I get the error. If I enter in just "15" with no decimal point, it works fine. I did look on java.sun.com for how to correct this and have not got gotten anywhere.

In terms of Type, what difference can you see between 15 and 15.05? Why do you think the compiler might complain about that?

I'm thinking I need to convert the integer to a double, but whichever way I try to go about it, I'm having no luck.

To put it another way, what Type is amount? And what kind of numbers does that type have to be. What can it NOT be?

Have you tried to handle the numbers on the left side of the decimal place separate from the numbers on the right? 12.15 --> 12 and 15?

Amount is set as an int because it has to be. It needs to be a double to give me the decimal points, correct? So what I have tried to do is create a parse method to convert the int to the double, but I keep getting error messages.

I have not tried to keep them separated simply because I already have no clue how to do this (can't you tell?) and I'm trying to keep it as simple as possible, though maybe I'm going about this all wrong.

Generally when I write out a check, I don't write "fifteen seventy five", I write "Fifteen and Seventy five cents" or "Seventy five and no cents". Your written out part doesn't need to spell it out in decimals does it? Could it format it the way I write my checks, splitting up the numbers on the right from the values on the left of the decimal point?

Let me repeat:
What statement gives the error?
What method is used in that statement?
What kind of data is acceptable to that method?
Are you entering the type of data the is correct for the method?

It doesn't have to be really really hard...

What kind of math do you think you need to do to get 15.75 (a double) into it's two integer values?

Try to store each of them as a leftSide and rightSide int variables (not to be confused with Lvalues/Rvalues).

hint:
1.) how do you get 15.75 to be just 15? (Think casting)

2.) 15.75 - what = .75? and .75 * what = 75?

brandonrunyon- the program has to write it such as "One thousand nine hundred twenty and 85 cents" or "One thousand nine hundred twenty dollars and 85 cents"- either way is okay. The cents do not have to be spelled out.

NormR1- I have tried creating a parse method to convert the integer to a double and when I try to run the program, I continue to get the InputMismatch Exception. Here is how I tried to create this:

int amount = 0; // To hold amount of check
double d = (double)amount; // Convert int to double amount

Let me repeat again:
What statement gives the InputMismatch Exception error?

would it be easier to type amount as a double and then take the steps to break it down into it's integer components for the whole convert method? The conversion method at the top does take an int only, doesn't it?

Member Avatar for coil

You don't need an explicit cast if you're converting from int to double, though I don't see how it would affect the program.

The best way to do it, summarizing what everyone else has said, would probably be:
1. Create two ints, one to hold the stuff on the left side of the decimal, and one to hold the stuff on the right side (keep in mind there might not be a decimal point). 2. Convert all the digits in the first int to words and append it to a String.
3. Append an "and" to the same String.
4. Append the second int.

commented: I tried, and yet I couldn't make that clearer. Excellent! +3

Why not times 100 and work in cents? $1 is 100 cents

Why not times 100 and work in cents? $1 is 100 cents

That would also work very well...

Her switch statements at the top change an int value to a written word but works in natural numbers. I thought it might be easier to run the left and right values to the decimal point as separate into the written to word switch and append as needed. But, maybe not...

What statement gives the error? When I enter in a decimal formatted number, the entire output gives this exception.
What method is used in that statement? This is the area of the code that must be the problem:
// Get amount of check from user
System.out.print("Please enter amount of check: ");
amount = keyboard.nextInt();

// Convert dollar amount to word amount
String inwords = w.convert(amount);

// Create StringBuilder and append strings
StringBuilder str = new StringBuilder();

str.append("Date: " +date+ "\n"); // Append date
str.append("Pay to the Order of: " +name+ "\n"); // Append payee
str.append("$" +amount+ "\n"); // Append amount

// Display output of check
System.out.println(str);
System.out.println(inwords+ "dollars");
What kind of data is acceptable to that method? I have to create the var amount as an int, but in order to allow it to produce the decimal point result, it needs to be a double, which is why I tried converting it as I posted a few minutes ago.

brandonrunyon- I tried looking up online how to convert an int to a double and the example that I found was this:
You can convert an integer to a real number (double or float) as follows:

int i = 10;
double d = (double)i; /* Convert int to double. */
float f = (float)f; /* Convert int to float. */

That's why I'm thinking I can do it without having to break up the code even more. If I do break it up, I will then need to create another array, correct?

I totally understand that 100 cents = $1, but being so new to Java, I'm very unsure of how to put all this in the code to make it give the correct results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.