PDA

View Full Version : JAVA Homework Help PLEASE!




mcmacmcmac
Oct 27, 2007, 12:12 PM
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



AJ Muni
Oct 27, 2007, 12:19 PM
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
Oct 27, 2007, 12:21 PM
The error basically tells you what to do! Your toString() method must have a return statement at the end. Even return ""; would do.

eg

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.

yg17
Oct 27, 2007, 12:26 PM
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
Oct 27, 2007, 12:30 PM
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.

mcmacmcmac
Oct 27, 2007, 12:35 PM
thanks for the replies guys, i think i got it

russellelly
Oct 27, 2007, 02:04 PM
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

public void toString()

?