John Jaspers

Greenhorn
+ Follow
since Mar 27, 2003
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Jaspers

I just started using JEdit and like the feature in CodeAid that lets you see a Javadoc popup for any member you select. To get it to work, you have to set the source paths in jane.
My problem is, I can't get it to work. Could someone tell me which source paths I have to add to get this work?
Any help would be much appreciated. Thanks.
O'Reilly has a new book on the very subject: Java Data Objects by David Jordan and Craig Russell. ISBN: 0-596-00276-9

APress has one also, called Using and Understanding Java Data Objects
By David Ezzio ISBN: 1-59059-043-0
Haven't checked them out, yet, but I plan to check them both out at some point.
Hope this helps.
18 years ago
I realize this is kind of a silly question, but what they heck: If I have a package whos directory structure is say C:\packages\com\whatever and I want to add to the whatever package, how would I do this and have the program find the package? Do I have to add this directory structure to my classpath, because using import statements doesn't seem to do it in JBuilder 8, and I can't figure out what I'm doing wrong.
And after getting it set up correctly, what do I use as the import line? import packages.com.whatever.*, or just com.whatever.*? How would I set it up so that it were only com.whatever.*?
Thanks in advance.
18 years ago
Thanks for the replies, everyone.
I will use all the advice.
18 years ago
I was curious as to whether using GridBag layout is the preferred way of laying out components. It seems that it's overkill for most GUIs, but I'm reading a book that says it is the most often used. In another book, it was said that it was mostly used by RAD programs.
So which is it? Can I do without it-for the most part-or should I learn it intimately?
Also, what are some good books for learning Swing?
[ June 18, 2003: Message edited by: John Jaspers ]
18 years ago
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.
18 years ago
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;
}
}
18 years ago
I'm sorry. I should have made my question clearer.
The following code is supposed to catch a NullPointerException in any of the JOptionPanes if one presses cancel. This works just fine in the first two, but the third seems to always try to assign to the int. The NullPointerException should occur before the assignment, but it seems to always occur at the assignment and throw a NumberFormatException.
Could anyone explain to me why this happens here and not in the two previous boxes?
Thanks again.
[ March 28, 2003: Message edited by: John Jaspers ]
18 years ago
I don't understand why this happens in this code.
The only difference I see is that the first two numbers are doubles and the third is an int, but the exception is caught for the first two and not the third. The third throws the NumberFormatException.
But if I hit Cancel BEFORE parseInt() is called, why am I getting a NumberFormatException?
I couldn't find anything in the docs that might explain this.
Thanks in advance.
import javax.swing.*;
import java.text.*;
public class FutureValue
{
public static void main(String[] args)
{
String choice = "";
try
{
while(!(choice.equalsIgnoreCase("x")))
{
//Works as expected here.
String paymentString = JOptionPane.showInputDialog(null,
"Enter monthly payment: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double monthlyPayment = Double.parseDouble(paymentString);

//Works as expected here.
String rateString = JOptionPane.showInputDialog(null,
"Enter yearly interest rate: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double interestRate = Double.parseDouble(rateString);
double monthlyInterestRate = interestRate/12/100;

//NumberFormatException thrown here.
String yearsString = JOptionPane.showInputDialog(null,
"Enter number of years: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
int years = Integer.parseInt(yearsString);
int months = years * 12;
double futureValue = calculateFutureValue(monthlyPayment,
months, monthlyInterestRate);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(2);
String message =
"Monthly payment: " + currency.format(monthlyPayment) + "\n"
+ "Yearly interest rate: " + percent.format(interestRate/100) + "\n"
+ "Number of years: " + years + "\n"
+ "Future value: " + currency.format(futureValue) + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
choice = JOptionPane.showInputDialog(null,
message, "Future Value", JOptionPane.PLAIN_MESSAGE);
}//end while
}//end try
catch(NullPointerException e)
{
System.exit(0);
}//end catch

System.exit(0);
}//end main
private static double calculateFutureValue(double monthlyPayment,
int months, double interestRate)
{
int i = 1;
double futureValue = 0;
while (i <= months)
{
futureValue = (futureValue + monthlyPayment) *
(1 + interestRate);
i++;
}//end while

return futureValue;

}//end calculateFutureValue
}//end class
18 years ago