1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

JAVA Homework Help PLEASE!

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

  1. macrumors member

    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

    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. macrumors 65816

    AJ Muni

    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. macrumors 6502

    The error basically tells you what to do! Your toString() method must have a return statement at the end. Even return ""; would do.

    	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:
    	 * 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. macrumors G5

    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:


    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. macrumors member

    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. macrumors member

    thanks for the replies guys, i think i got it
  7. macrumors regular

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

    public void toString() 

Share This Page