PDA

View Full Version : Another JOptionPane problem


gufmn
09-04-2002, 04:51 PM
OK, I'm working on the same project as inkedmn. It basically uses JOptionPane prompting the user 3 times for input. It then does some simple calculation and displays it in another dialog box.
Btw, this is an applet.
Problem is: Every time run the applet, be it through the browser or through the appletviewer, it freeks out. The first box pops up asking for input. Then, as soon as you click on "ok" (or anything else for that matter) the box closes for a split second and re-opens again. Over and over and over. I can see the second input box behind it durring the split second the first one is closing and re-opening but can't get to it.

This is the strangest part:

I have found that when I execute it through the appletviewer, and, if and only if I minimize the ms-dos "APPLET" screen before I try to type in the input field, it works as it's supposed to. :wtf:

Anyway, here's my code:




import javax.swing.*;
import java.applet.*;
import java.awt.Graphics;
import java.util.*;
import java.text.NumberFormat;

public class MovieCashierApplet extends JApplet {
public void paint(Graphics g) {
Locale myLocale = new Locale("us", "US");
NumberFormat nf = NumberFormat.getCurrencyInstance(myLocale);
float PRICE1 = (float)8.75;
float PRICE2 = (float)5.50;
double TAX_RATE = .0775;
double STUDENT_DISCOUNT = .10;
String CHOICE = JOptionPane.showInputDialog("Hello! Welcome to the Movie Cashier\n\n1) Martin Lawerance Live: Runteldat (R)\n2) The Last Kiss (R)\n\nPlease select a movie:" , "Enter the number of the film:");
int MOVIE_CHOICE = Integer.parseInt(CHOICE);
String ADULTS = JOptionPane.showInputDialog(null,"Please enter the number of adult tickets", "Number of adults");
int NUM_ADULTS = Integer.parseInt(ADULTS);
String KIDS = JOptionPane.showInputDialog(null,"Please enter the number of child tickets", "Number of children");
int NUM_KIDS = Integer.parseInt(KIDS);
float SUBTOTAL = ((float)(NUM_ADULTS * PRICE1) + (float)(NUM_KIDS * PRICE2));
float TAXTOTAL = (float)(SUBTOTAL * TAX_RATE);
float TOTAL = (float)(SUBTOTAL + TAXTOTAL);
float DISCTOTAL =(float)(TOTAL * .9);
JOptionPane.showMessageDialog(null,"Subtotal is $" + nf.format(SUBTOTAL) + "\nSales tax is $" + nf.format(TAXTOTAL) + "\nTotal ammount due is $" + nf.format(TOTAL) + "\n\nStudent discounted price is $" + nf.format(DISCTOTAL));


}

}

why does the indentation get all screwed up?

Dru Lee Parsec
09-04-2002, 07:39 PM
First of all, fix that text formatting.

Your showInputDialog line doesn't compile on my computer because it doesn't have a parent component.

This code seems to work.


import javax.swing.*;
import java.applet.*;
import java.awt.Graphics;
import java.util.*;
import java.text.NumberFormat;

/*
Trick: Put the applet tag right here inside a comment
Then test your code like this:

appletviewer MovieCashierApplet.java

<applet code="MovieCashierApplet" width="150" height="150" alt=""></applet>
*/

public class MovieCashierApplet extends JApplet {
public void paint(Graphics g) {
Locale myLocale = new Locale("us", "US");
NumberFormat nf = NumberFormat.getCurrencyInstance(myLocale);
float PRICE1 = (float)8.75;
float PRICE2 = (float)5.50;
double TAX_RATE = .0775;
double STUDENT_DISCOUNT = .10;
String msg1 = "Hello! Welcome to the Movie Cashier\n\n";
msg1 += "1) Martin Lawerance Live: Runteldat (R)\n";
msg1 += "2) The Last Kiss (R)\n\nPlease select a movie:";
String CHOICE = JOptionPane.showInputDialog(null,
msg1 ,
"Enter the number of the film:",
JOptionPane.INFORMATION_MESSAGE);

int MOVIE_CHOICE = Integer.parseInt(CHOICE);
String ADULTS = JOptionPane.showInputDialog(null,
"Please enter the number of adult tickets",
"Number of adults",
JOptionPane.INFORMATION_MESSAGE);

int NUM_ADULTS = Integer.parseInt(ADULTS);
String KIDS = JOptionPane.showInputDialog(null,
"Please enter the number of child tickets",
"Number of children",
JOptionPane.INFORMATION_MESSAGE);

int NUM_KIDS = Integer.parseInt(KIDS);
float SUBTOTAL = ((float)(NUM_ADULTS * PRICE1) + (float)(NUM_KIDS * PRICE2));
float TAXTOTAL = (float)(SUBTOTAL * TAX_RATE);
float TOTAL = (float)(SUBTOTAL + TAXTOTAL);
float DISCTOTAL =(float)(TOTAL * .9);

String msg2 = "Subtotal is $" + nf.format(SUBTOTAL);
msg2 += "\nSales tax is $" + nf.format(TAXTOTAL);
msg2 += "\nTotal ammount due is $" + nf.format(TOTAL);
msg2 += "\n\nStudent discounted price is $" + nf.format(DISCTOTAL);

JOptionPane.showMessageDialog(null,msg2);
}
}


Normally I wouldn't do homework for you ;) , but the changes
were actually pretty minor. I think your showInputDialog was what was messed up.

Also, I'd change the variable names PRICE1 and PRICE2 to
priceChild and priceAdult.

And as long as we're talking about variable names, the standard
in Java is for class names to have an upper case initial letter and
upper case initial letter for each additional word in your class
name. So MovieCashierApplet is correct.

But method and variable names all start with lower case letters.
So myLocale is correct but PRICE1 is very wrong from a style
point of view. All upper case variable names are usually only
used for static final attributes. such as

public static final int MAXIMUM_USERS = 50;

gufmn
09-04-2002, 08:34 PM
Thanks for the help. I'll give it a shot and let you know how it goes.
We were actually told by the unstructor to use ALL CAPS for variable names. That's why I did it that way. I won't be doing it for non-class projects.

Dru Lee Parsec
09-04-2002, 08:47 PM
We were actually told by the unstructor to use ALL CAPS for variable names.

Wow! Has he actually looked at the Java API?

That's just wrong. That's amazing that he's teaching you such a terrible habit that NOBODY in the real world does.

I tell ya, it makes me want to retire from writing code and go to work teaching Java.

<edit>
Found it! Point him here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variableNames.html

inkedmn
09-04-2002, 09:33 PM
/me would like to hava Dru for a java teacher :)

kmj
09-04-2002, 10:54 PM
That's horrible.. I'm starting to worry about the value of this class, if your professor would actually tell you to do that! Are you sure they weren't referring to just constant variables? If not, then you should have your java teacher have a little talk with [b]every other coder in the world![/b[

:)

Good night; god bless.

gufmn
09-05-2002, 01:32 PM
My bad. Had another class last night and he explained that he was referring to constant variables. After only 3 meetings, I don't think he had time to thoroughly explain it until last night.

So anyway, he doesn't seem to be an idiot :)

kmj
09-05-2002, 02:21 PM
phew; that's a relief!8)