When I try to run what I have so far in my program, I get an error when I enter in the date as 11/11/2010 that states "Exception in thread "main" java.util.InputMismatchException". Why am I getting this and how do I fix it? Also, I have some comments in the program for things I still need to do, but am a bit stuck as to where to go from here concerning the code. Any input is appreciated!

Write a program that displays a simulated paycheck.
			    The program should ask the user to enter the date,
			    the payee's name, and the amount of the check.  It
			    should then display a simulated check with the
			    dollar amount spelled out as shown here:

				   			                       Date:  11/24/2007

			    Pay to the Order of:   John Phillips	    $1920.85

			    One thousand nine hundred twenty and 85 cents

*/

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

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
		int date = 01/01/2010;		// To hold the date
		String name = "";			// To hold the payee name
		double amount = 10.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.nextInt();

		// 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: ");					// Append date
		str.append("Pay to the Order of: ");	// Append payee
		str.append("$ ");						// Append amount
		str.append(" ");						// Append word amount

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

		// Exit system
		System.exit(0);

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

}	// End public class checkWriter

Recommended Answers

All 68 Replies

From the API doc for Scanner.nextInt()

Throws:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

"11/11/2010" does not look much like an int.

Observe carefully the chunk of code which tries to read the date :-

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

Now the program here is expecting an integer value, However you are feeding it 11/11/2010 Now that doesn't look like an integer to me from any perspective, Hence the 'java.util.InputMismatchException', when you enter the date, try using just an integer value like '10', it will go on and ask the payee name.
Take a look at the javadocs of the Scanner class, it should help you get an insight how its methods behave.

Member Avatar for coil

By the way, I'm not sure why this is compiling.

int date = 01/01/2010;

That's not a int, that's an invalid character from the compiler's point of view. If you want to do that, you could declare three ints, each storing one part of a date (e.g. date, month), or, you could create a class that stores a date.

int date = 01/01/2010;

Would it compile better if we changed it to
int date = 01+01+2010;

int date = 01/01/2010; divides out to zero, so the compiler has no problem with it.

For the variable date should we define as String?

String date = "01/01/2010"; // To hold the date

The code in line 33 would be:

date = keyboard.nextLine(); // To receive the date as a String

01/01/2010 would be more of a "date" type than an int...

I would consider accepting three integers, 1 for day value, another for month value, and finally one for the year. You could then concatenate all three values with a string or char = '/' in between your three values. like this: "Enter the day: " 01-->day, "Enter the month: " 01--> month, "Enter the year: " 2010 --> year, then print them like this: System.out.println(month + "/" + day + "/" + year); Just a thought if they must be ints. Scanner class might provide more appropriate features for dates.

Ok, so I made some changes and got things to compile correctly and let me enter in the information. However, now it is not returning anything at the end. Here is what I have:

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


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
		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 StringTokenizer object
		StringTokenizer strTokenizer = new StringTokenizer(date, "/");
		date = strTokenizer.nextToken();

		

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

		

		

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

		

		

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

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

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

		

		// Exit system
		System.exit(0);

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

}	// End public class checkWriter

You are assigning your variables right, but at the end you don't even use those variables in you output. You System.out.print(str), but str doesn't have your date, or name, or amount.

I thought that's what I was doing in the string builder and append areas of the program?

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

But your tokenizer is probably unnecessary as it truncates the date formating. just a plain ol' string without the tokenizer object would do the trick I think since you no longer type date as int.

Ok, I see what you're doing. I was trying to figure all this out with missing pieces. Hopefully this will work now!

I do have one other question. How do I make the program print out the dollar amount that was entered in into text also? For example, if the user enters in $15.50, how do I make the "$15.50" show, plus have it written out as "fifteen dollars and fifty cents"?

Yeah... concatenate the strings around your variables when you append them.
You basically had it. Just missing a few things.

how do I make the "$15.50" show, plus have it written out as "fifteen dollars and fifty cents"

You need to convert the String the user entered to an int value and then use some logic to convert the int value to the String you want.
A hint: sometimes its easier to work with whole cents vs 1/100 of a dollar. For example use 1550 cents vs 15.50 dollars

Create a NumberFormat object and use the getCurrencyInstance() method

NumberFormat nf = NumberFormat.getCurrencyInstance();

import it from java.text.NumberFormat

Then any number you enter, int or double, will be formated to $x.00

str.append(nf.format(amount)+"\n");// Append amount

It will even put the "," place holders in for values like 1000000 ==> $1,000,000.00

I thought I would use the String valueOf method to convert it, so I had it entered in as:

System.out.println(String.valueOf(amount));

However, this keeps giving me compile errors. I have also tried entering in double amount, and it didn't like it either. What am I missing here?

keeps giving me compile errors

Please copy and paste the full text of the error message.The message will tell you what the problem is.

NormR1's way is more exciting though... you may want to at least attempt his suggestion.

Here is the error:

C:\Users\Carter\Documents\checkWriter.java:63: cannot find symbol
symbol : method valueOf(double)
location: class java.lang.String
System.out.println(String.valueOf(amount));

I need to create the program so that the user enters in the date, payee, and dollar amount, as for example, $15.78. The program then needs to take that $15.78 and automatically convert it to the written amount. I've also tried doing Double.toString(amount) and it doesn't give me any compile errors, but it won't display this when I try to pass it in the append area either.

Do you mean like 15.75 to "Fifteen Seventy five"?

valueOf(d); does not return a string literal english representation of a double passed... it returns a Double object. String.valueOf(d) would only take the Double object and cast it to a String representation. So those two alone don't appear to do any sort of 'translation' as you desire.

valueOf(d); does not return a string literal english representation of a double passed... it returns a Double object.

What class is valueOf() in? The API doc gives:
public static String valueOf(double d)
Returns the string representation of the double argument.

String.valueOf(d) would only take the Double object and cast it to a String representation

It converts the double, not casts it.

commented: good catch +3

Semantics...

The string representation in this case is not 50 = string "Fifty", it's int 50 = string "50"; and I gather now that the problem is she wants it to write out in english 'fifty' if the user enters 50.

Though you're right, conversion and cast are different and I should know better...

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.