Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
Hey, can anyone spot why this thing won't compile. I just don't get it, and it's driving me crazy!!

I get 1 error

59: missing return statement
} // end method toString
^
1 error

Code:
import java.util.Scanner; // program uses class Scanner


public class Invoice
{
	private String customerName; // name of customer this Invoice is issued to
	private double invoiceTotal; // total amount of the customer's purchases

	// custructor
	public Invoice ( String name)
	{
		customerName = name; // initializes customerName
	} // end constructor

	// method to retrieve the customer name
	public String getCustomerName()
	{
		return customerName;
	} // end method getCustomerName

	// method to retrieve the invoice total
	public double getInvoiceTotal()
	{
		return invoiceTotal; // gives the value of invoice total to the calling method
	} // end method getinvoiceTotal

	// method to obtain user input

	public void addItem()
	{
		// create Scanner to obtain input from command window
		Scanner input = new Scanner ( System.in );

		// prompt user to unter the unit price and quantity for each item
		// prompt user to enter <ctrl> z once all items have been entered
		System.out.print("Enter the unit price and quantity purchased or <ctrl> z to quit:");
		while ( input.hasNext() )
		{
			double unitPrice = input.nextDouble();
			int quantity = input.nextInt();
			invoiceTotal += (unitPrice*quantity);
		} //end while
	} // end method addItem

	public String toString()
	{
		System.out.printf("Customer Name: %s\n",getCustomerName() );
		System.out.printf("Invoice Total: %.2f\n",getInvoiceTotal() );
	} // end method toString
} // end class Invoice
 

AJ Muni

macrumors 65816
Aug 4, 2005
1,153
24
Miami
I'm only in java 1, so dont take me too serious. But i've never seen that System.out.printf before... with the " f ". I've only seen, and used, System.out.println (to start a new line) and System.out.print (to continue on same line). I may be dead wrong of course.

Edit: I think your missing the +.

System.out.printf("Customer Name: %s\n" + getCustomerName() );
System.out.printf("Invoice Total: %.2f\n" + getInvoiceTotal() );
 

iJed

macrumors 6502
Sep 4, 2001
264
10
West Sussex, UK
The error basically tells you what to do! Your toString() method must have a return statement at the end. Even return ""; would do.

eg
Code:
	public String toString()
	{
		System.out.printf("Customer Name: %s\n",getCustomerName() );
		System.out.printf("Invoice Total: %.2f\n",getInvoiceTotal() );
		
		return "";
	}

However, what it really should do though is something like this:
Code:
	/**
	 * My javadoc compliant comment
	 */
	public String toString()
	{
		return String.format("Customer Name: %s\nInvoice Total: %.2f\n", getCustomerName(), getInvoiceTotal());
	}

Also I would be hesitant to put anything like "} // end method toString" at the end of each method or class. This is the type of thing a verbose language purist would tell you to do and it makes the code no more readable than it was without.
 

yg17

macrumors Pentium
Aug 1, 2004
15,028
3,003
St. Louis, MO
Just for the sake of good programming practice, the toString() method should return a string, rather than output it, then in the call to it, you would have:

System.out.println(blah.toString());


The toString() method should never actually output anything. If you want the method to output something, change the return type to void and then I think you'll have to rename the method because I don't think you can overload the default toString() method like that.
 

mcmacmcmac

macrumors member
Original poster
Oct 2, 2007
86
0
This is what is asked in the guidelines:

o Have a method called toString, which when called, will print out the name and invoice total of the customer rounded to two decimal places.
 

russellelly

macrumors regular
Jun 23, 2006
139
41
Glasgow, UK
This is what is asked in the guidelines:

o Have a method called toString, which when called, will print out the name and invoice total of the customer rounded to two decimal places.

If that's the case then shouldn't you be using the header

Code:
public void toString()

?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.