JAVA Homework Help PLEASE!

Discussion in 'Mac Programming' started by mcmacmcmac, Oct 27, 2007.

  1. mcmacmcmac
    Expand Collapse
    macrumors member

    Joined:
    Oct 2, 2007
    #1
    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
     
  2. AJ Muni
    Expand Collapse
    macrumors 65816

    AJ Muni

    Joined:
    Aug 4, 2005
    Location:
    Miami
    #2
    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() );
     
  3. iJed
    Expand Collapse
    macrumors 6502

    Joined:
    Sep 4, 2001
    Location:
    West Sussex, UK
    #3
    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.
     
  4. yg17
    Expand Collapse
    macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #4
    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.
     
  5. mcmacmcmac
    Expand Collapse
    thread starter macrumors member

    Joined:
    Oct 2, 2007
    #5
    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.
     
  6. mcmacmcmac
    Expand Collapse
    thread starter macrumors member

    Joined:
    Oct 2, 2007
    #6
    thanks for the replies guys, i think i got it
     
  7. russellelly
    Expand Collapse
    macrumors regular

    Joined:
    Jun 23, 2006
    Location:
    Glasgow, UK
    #7
    If that's the case then shouldn't you be using the header

    Code:
    public void toString() 
    ?
     

Share This Page