• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!