- #1
FritoTaco
- 132
- 23
Hello,
I am writing a program that takes a users input of how many credits they're taking, and the number of dollars each credit is. The book and parking fee is a constant number (no change). I'm using JOptionPane so users can enter 2 values, 1 for number of credits, and the other for dollars per credit. I want to enter a number such as; 135.65 into the input. When I do that I get (you can see in the screenshot) a blank applet. When I use just integers it runs fine and outputs total and everything, except when I enter a decimal it doesn't execute properly. I'm new to Java Applet and was wondering why can't 'double' work. Also, how can I fix it so I can enter decimal numbers? I'm using BlueJ if you're wondering.
I am writing a program that takes a users input of how many credits they're taking, and the number of dollars each credit is. The book and parking fee is a constant number (no change). I'm using JOptionPane so users can enter 2 values, 1 for number of credits, and the other for dollars per credit. I want to enter a number such as; 135.65 into the input. When I do that I get (you can see in the screenshot) a blank applet. When I use just integers it runs fine and outputs total and everything, except when I enter a decimal it doesn't execute properly. I'm new to Java Applet and was wondering why can't 'double' work. Also, how can I fix it so I can enter decimal numbers? I'm using BlueJ if you're wondering.
Java:
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
public class Tuition extends JApplet {
int number1, number2, number3, number4;
int sum;
int NOC; // Number of credits
double DPC; // dollars per credit
int PF; // Parking fee
int BF; // Book(s) fee
int Multiply;
int total;
String numberInStringFormat;
String Message = "Tuition Fee for school";
public void init() {
String input;
input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter Number of Credits", JOptionPane.QUESTION_MESSAGE);
number1 =Integer.parseInt(input);
input =JOptionPane.showInputDialog (null, numberInStringFormat, "Enter cost Per Credit", JOptionPane.QUESTION_MESSAGE);
number2 =Integer.parseInt(input);
sum = 60 + 400; // parking fee + book fee
Multiply = number1 * number2; // number of credits * dollars per credit
total = Multiply + sum;
NOC = number1;
DPC = number2;
JOptionPane.showMessageDialog(null, "Total Fee = " +total);
}
public void paint(Graphics g) {
Font f = new Font("Serif", Font.PLAIN, 16);
Font fb = new Font("TimesRoman", Font.BOLD, 18);
g.setFont(f); //set new font
g.drawString("Number of Credits entered: " + NOC, 70, 220);
g.drawString("Dollars per credit: " + DPC, 70, 240);
g.drawString("Parking fee amount: 60", 70, 260);
g.drawString("Book(s) fee: 400", 70, 280);
g.setFont(fb);
g.drawString("Total cost: $"+total, 70, 310); //total Number of credits * dollars per credit + Book/parking fee.
}
}
Attachments
Last edited: