• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
John Jaspers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic