Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes problem with toString() in Sun ONE Studio 4, update 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with toString() in Sun ONE Studio 4, update 1" Watch "problem with toString() in Sun ONE Studio 4, update 1" New topic
Author

problem with toString() in Sun ONE Studio 4, update 1

John Jaspers
Greenhorn

Joined: Mar 27, 2003
Posts: 9
The original BookOrder class did not have any methods, or variables, to handle dates(I'm working out of Murach's "Beginning Java 2"). The assignment was to add these things, including the "Date: " + date + "\n\n" line to the toString() method. The resulting code is almost exactly like the example given in the book, yet when the main class calls toString(), all the lines after the "Date: " + date + "\n\n" line runs.
I've tried running the debugger to see what is happening, but Sun ONE locks up whenever I run it.
This is driving me nuts, because I don't think the code is wrong. Any help is appreciated.
//BOOKORDER CLASS
import java.util.*;
import java.text.*;
public class BookOrder{
private Book book;
private int quantity;
private double total;
private String date;
public BookOrder(String bookCode, int bookQuantity){
book = new Book(bookCode);
quantity = bookQuantity;
setTotal();
setDate();
}
public void setTotal(){
total = quantity * book.getPrice();
}

public void setDate()
{
// Get current date.
GregorianCalendar gregDate = new GregorianCalendar();

// Convert to millisecond form.
Date now = gregDate.getTime();

// Create a date format.
DateFormat dtfDate = DateFormat.getDateInstance(DateFormat.SHORT);

// Assign date format to string.
date = dtfDate.format(now);

}//end setDate()

public String getDate()
{
return (date);
}//end getDate()
public Book getBook(){
return book;
}
public int getQuantity(){
return quantity;
}
public double getTotal(){
return total;
}
// "Date: " + date + "\n\n" does not display.
public String toString(){
NumberFormat currency = NumberFormat.getCurrencyInstance();
String orderString = "Date: " + date + "\n\n"
+ "Code: " + book.getCode() + "\n"
+ "Title: " + book.getTitle() + "\n"
+ "Price: " + currency.format(book.getPrice()) + "\n"
+ "Quantity: " + quantity + "\n"
+ "Total: " + currency.format(total) + "\n";
return orderString;
}
}
MAIN CLASS
import javax.swing.JOptionPane;
public class BookOrderApp{
public static void main(String[] args){
String choice = "";
try{
while (!(choice.equalsIgnoreCase("x"))){
String title = JOptionPane.showInputDialog(
"Enter a book code:");
String inputQuantity = JOptionPane.showInputDialog(
"Enter a quantity:");
int quantity = parseQuantity(inputQuantity);
BookOrder bookOrder = new BookOrder(title, quantity);
String message = bookOrder.toString() + "\n"
+ "Press Enter to continue or enter 'x' to exit:";
choice = JOptionPane.showInputDialog(null,
message, "Book Order", JOptionPane.PLAIN_MESSAGE);
}//end while
}
catch(NullPointerException e){
System.exit(0);
}
System.exit(0);
}
public static int parseQuantity(String quantityString){
int quantity = 0;
boolean tryAgain = true;
while(tryAgain){
try{
quantity = Integer.parseInt(quantityString);
tryAgain = false;
}
catch(NumberFormatException e){
quantityString = JOptionPane.showInputDialog(null,
"Invalid quantity. \n"
+ "Please enter an integer.");
}
}
return quantity;
}
}
Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
Sorry I don't have the solution to your problem, but will chime in and mention that the Sun One debugger was hanging on me also, so I abandoned it.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I have just run your program in the BlueJ learning IDE, it works fine and displays the date. I had to create the simple Book class.
So maybe BlueJ is what you should be using


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Jaspers
Greenhorn

Joined: Mar 27, 2003
Posts: 9
I was also able to run it in BlueJ, but it did something to the file that made it unrunnable in Sun ONE. In the end, I just downloaded JBuilder 8 Personal. I don't seem to have any problems-yet-with this one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with toString() in Sun ONE Studio 4, update 1
 
Similar Threads
static method data validation in a dialog box app
Custom Date class and Appointment class
How do I use a (boolean?) token to reject user input?
HELP needed on DATE
help getting user supplied dates to display