Percents calculating part does not work in java - java

[Now working!] I have this code that should calculate percents (With some GUI). But the calculating part is not working... Maybe some of you know the answer? I think the problem is when I concert it from a string do a double. I'm from Sweden so I have a very crappy english. And all variable names were in swedish so I just translated them with googles translate.
package Java;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class java {
public static void main (String []args){
int frameSizeX = 500;
int frameSizeY = 135;
JPanel p = new JPanel();
p.setPreferredSize( new Dimension(frameSizeX,frameSizeY) );
JFrame f = new JFrame ("Percentage");
f.setSize(frameSizeX,frameSizeY);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.getContentPane().add(p);
JLabel cardiNum = new JLabel ("Cardinal number");
JTextField cardiNumField = new JTextField();
cardiNumField.setPreferredSize( new Dimension( 500, 24 ) );
final String talValue = (cardiNumField.getText());
JLabel perc = new JLabel("Percentage");
JTextField percField = new JTextField();
percField.setPreferredSize( new Dimension( 500, 24 ) );
final String proValue = (percField.getText());
double D = 0;
final JLabel answer = new JLabel("The answer is = " + D);
JButton butt = new JButton("Calculate");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double A = Double.parseDouble(talValue);
double B = Double.parseDouble(proValue);
double C = A/100;
double D = C*B;
answer.setText("The answer is" + D);
}
});
p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
p.add(perc, BorderLayout.AFTER_LINE_ENDS);
p.add(percField, BorderLayout.AFTER_LINE_ENDS);
p.add(butt);
p.add(answer);
f.pack();
f.setVisible(true);
}
}

You're getting the NullPointerException because you need to set those two strings only once the JButton has been clicked, inside the ActionListener.
Put these two lines:
final String talValue = (cardiNumField.getText());
final String proValue = (percField.getText());
inside the ActionListener for the JButton and make the cardiNumField and percField final and it should work. Alternatively, you can just remove those two variables and have:
double A = Double.parseDouble(cardiNumField.getText());
double B = Double.parseDouble(percField.getText());

As Martin Dinov mentions, the problem is that you were reading the contents of the text boxes before the user was able to write in them and click the button. That leads to a NumberFormatException, because you're trying to convert the empty String "" to a double. Therefore, you need to put that code inside the button click event handler. An extra change to fix this is that you need to make your text fields final. i.e.
final JTextField cardiNumField = new JTextField();
final JTextField percField = new JTextField();
That's because the button click handler is an anonymous inner class, so it can only read from final variables.
The following code fixes the problem I think you have been seeing.
public static void main(String[] args) {
int frameSizeX = 500;
int frameSizeY = 135;
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(frameSizeX, frameSizeY));
JFrame f = new JFrame("Percentage");
f.setSize(frameSizeX, frameSizeY);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.getContentPane().add(p);
JLabel cardiNum = new JLabel("Cardinal number");
final JTextField cardiNumField = new JTextField();
cardiNumField.setPreferredSize(new Dimension(500, 24));
JLabel perc = new JLabel("Percentage");
final JTextField percField = new JTextField();
percField.setPreferredSize(new Dimension(500, 24));
double D = 0;
final JLabel answer = new JLabel("The answer is = " + D);
JButton butt = new JButton("Calculate");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
double cardinal = Double.parseDouble(cardiNumField.getText());
double percentage = Double.parseDouble(percField.getText());
double C = cardinal / 100;
double D = C * percentage;
answer.setText("The answer is " + D);
}
});
p.add(cardiNum, BorderLayout.AFTER_LINE_ENDS);
p.add(cardiNumField, BorderLayout.AFTER_LINE_ENDS);
p.add(perc, BorderLayout.AFTER_LINE_ENDS);
p.add(percField, BorderLayout.AFTER_LINE_ENDS);
p.add(butt);
p.add(answer);
f.pack();
f.setVisible(true);
}
Good luck with learning Java. You're obviously very new to it! :)

Related

Why is setText of JTextField not working in the actionPerformed method?

im making a currency calculator and i have an issue with not being able to set the result of the calculation into the result JTextField, which is the object otherTextField.
Here is my class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ValutaKalkulator implements ActionListener {
private double nokValue;
private double otherValue;
private JButton buttonRemoveNok;
private JButton removeOther;
private JButton removeBoth;
private JButton exitButton;
private JButton usdButton;
private JButton sekButton;
private JButton gbpButton;
private JButton eurButton;
private JLabel nokLabel;
private JTextField nokTextField;
private JLabel otherLabel;
private JTextField otherTextField;
public ValutaKalkulator()
{
JFrame frame = new JFrame();
frame.setTitle("VALUTAKALKULATOR");
buttonRemoveNok = new JButton("Fjern NOK");
removeOther = new JButton("Fjern annen valuta");
removeBoth = new JButton("Fjern begge");
exitButton = new JButton("Avslutt");
usdButton = new JButton("USD");
sekButton = new JButton("SEK");
gbpButton = new JButton("GBP");
eurButton = new JButton("EUR");
nokLabel = new JLabel("NOK");
JTextField nokTextField = new JTextField();
otherLabel = new JLabel("Annen valuta");
otherTextField = new JTextField();
buttonRemoveNok.addActionListener(this);
removeOther.addActionListener(this);
removeBoth.addActionListener(this);
exitButton.addActionListener(this);
usdButton.addActionListener(this);
sekButton.addActionListener(this);
gbpButton.addActionListener(this);
eurButton.addActionListener(this);
JPanel pnlSouth = new JPanel(new GridLayout(1, 4));
JPanel pnlCenter = new JPanel(new GridLayout(2, 2));
JPanel pnlNorth = new JPanel(new GridLayout(1, 4));
pnlNorth.add(nokLabel);
pnlNorth.add(nokTextField);
pnlNorth.add(otherLabel);
pnlNorth.add(otherTextField);
pnlCenter.add(gbpButton);
pnlCenter.add(eurButton);
pnlCenter.add(usdButton);
pnlCenter.add(sekButton);
pnlSouth.add(buttonRemoveNok);
pnlSouth.add(removeOther);
pnlSouth.add(removeBoth);
pnlSouth.add(exitButton);
frame.add(pnlNorth, BorderLayout.NORTH);
frame.add(pnlSouth, BorderLayout.SOUTH);
frame.add(pnlCenter, BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
}
public String calculateFromNok(String nokValueString, String toValue)
{
double result = 0;
double nokValue = Double.valueOf(nokValueString);
switch(toValue)
{
case "GBP":
result = nokValue * 10.8980 / 100;
break;
case "EUR":
result = nokValue * 9.2450 / 100;
break;
case "USD":
result = nokValue * 8.5223 / 100;
break;
case "SEK":
result = nokValue * 96.48 / 100;
break;
}
String resultString = Double.toString(result);
return resultString;
}
public void actionPerformed(ActionEvent event)
{
String text = event.getActionCommand();
switch(text)
{
case "USD":
String txt = nokTextField.getText();
String txt2 = calculateFromNok(txt, "USD");
otherTextField.setText(txt2);
break;
}
}
}
I know the currencies are not correct and there are other logical flaws and bad variable names, but my question for now is why can i not put the result from the method calculateFromNok into my JTextField otherTextField?
Help appreciated thx!
You are creating an initialize local variable JTextField nokTextField.
So private JTextField nokTextField; is not initializing. That's why it's giving you nullpointerexception.
Just Change At Line Number 37:
JTextField nokTextField = new JTextField();
To
nokTextField = new JTextField();
Your issue is with the line:
JTextField nokTextField = new JTextField();
Change it to:
nokTextField = new JTextField();
The reason this happens is that you previously defined nokTextField at the top, and then you initialized a separate variable with the same name in the constructor. Using an IDE such as Eclipse makes these types of errors very easy to catch. I suggest you use one.

JLabel not appearing on panel

For class I'm supposed to be creating an application that first lets you choose which value you'd like to calculate, then asks to enter the appropriate info. Then when you click "calculate", it SHOULD display the answer. For some reason my JLabel that should be displaying the answer isn't showing up. I've been searching for a solution, but every thing I do, nothing appears after you click "calculate". I am a novice, please help :(
package decay.application;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DecayApplication implements ActionListener {
JFrame frame;
JPanel content;
JLabel prompt1, prompt2, prompt3, prompt4, displayFinal, displayIntitial, displayConstant, choose;
JTextField enterFinal, enterInitial, enterConstant, enterElapsed;
JButton finButton, inButton, conButton, calculate1, calculate2, calculate3;
public DecayApplication(){
frame = new JFrame("Decay Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content = new JPanel();
content.setLayout(new GridLayout(0, 2, 10, 5));
content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
choose = new JLabel("Which would you like to calculate?");
content.add(choose);
finButton = new JButton("Final Amount");
finButton.setActionCommand("finalAmount");
finButton.addActionListener(this);
content.add(finButton);
inButton = new JButton("Initial Amount");
inButton.setActionCommand("initialAmount");
inButton.addActionListener(this);
content.add(inButton);
conButton = new JButton("Constant");
conButton.setActionCommand("constant");
conButton.addActionListener(this);
content.add(conButton);
frame.setContentPane(content);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){new DecayApplication();}
public void actionPerformed(ActionEvent event) {
String clicked1 = event.getActionCommand();
String clicked2 = event.getActionCommand();
if (clicked1.equals("finalAmount")) {
prompt1 = new JLabel("Enter the initial amount:");
content.add(prompt1);
enterInitial = new JTextField(10);
content.add(enterInitial);
prompt2 = new JLabel("What's the constant?:");
content.add(prompt2);
enterConstant = new JTextField(10);
content.add(enterConstant);
prompt3 = new JLabel("How many years have elapsed?:");
content.add(prompt3);
enterElapsed = new JTextField(10);
content.add(enterElapsed);
calculate1 = new JButton("Calculate");
calculate1.setActionCommand("Calculate");
calculate1.addActionListener(this);
content.add(calculate1);
displayFinal = new JLabel(" ");
displayFinal.setForeground(Color.red);
content.add(displayFinal);
frame.pack();
if (clicked2.equals("Calculate")){
double finalAmount;
String e1 = enterInitial.getText();
String e2 = enterConstant.getText();
String e3 = enterElapsed.getText();
finalAmount = (Double.parseDouble(e1) + 2.0);
displayFinal.setText(Double.toString(finalAmount));
}
}
}
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
DecayApplication decay = new DecayApplication();
}
}
Here's your method actionPerformed:
public void actionPerformed(ActionEvent event) {
String clicked1 = event.getActionCommand();
String clicked2 = event.getActionCommand();
if (clicked1.equals("finalAmount")) {
prompt1 = new JLabel("Enter the initial amount:");
content.add(prompt1);
enterInitial = new JTextField(10);
content.add(enterInitial);
prompt2 = new JLabel("What's the constant?:");
content.add(prompt2);
enterConstant = new JTextField(10);
content.add(enterConstant);
prompt3 = new JLabel("How many years have elapsed?:");
content.add(prompt3);
enterElapsed = new JTextField(10);
content.add(enterElapsed);
calculate1 = new JButton("Calculate");
calculate1.setActionCommand("Calculate");
calculate1.addActionListener(this);
content.add(calculate1);
displayFinal = new JLabel(" ");
displayFinal.setForeground(Color.red);
content.add(displayFinal);
frame.pack();
//here should the if-loop end, because here is the end of instructions which should be called after clicking on the button
}
//and here the second if-loop
if (clicked2.equals("Calculate")){
double finalAmount;
String e1 = enterInitial.getText();
String e2 = enterConstant.getText();
String e3 = enterElapsed.getText();
finalAmount = (Double.parseDouble(e1) + 2.0);
displayFinal.setText(Double.toString(finalAmount));
}

rounding a number to the hundredth place

I want to round the output to the hundredth place but have failed to do so.
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Assignment2Part2 extends JFrame{
//window
private static final int WIDTH = 550;
private static final int HEIGHT = 400;
private JLabel firstNameL, lastNameL, milesL, costL, mpgL, dailycostL;//labels for all the variables
private JTextField firstNameTF, lastNameTF, milesTF, costTF, mpgTF, dailycostTF;//text fields for all the variables
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
public Assignment2Part2 ()
{
setTitle("Find your daily cost of driving");
//labels
firstNameL = new JLabel("First Name ", SwingConstants.CENTER);
lastNameL = new JLabel("Last Name ",SwingConstants.CENTER);
milesL = new JLabel("Total miles driven per day ",SwingConstants.CENTER);
costL = new JLabel("Cost per gallon of gas ",SwingConstants.CENTER);
mpgL = new JLabel("Average MPG ",SwingConstants.CENTER);
dailycostL = new JLabel("Daily cost of driving is: ",SwingConstants.CENTER);
//text fields
firstNameTF = new JTextField();
lastNameTF = new JTextField();
milesTF = new JTextField();
costTF = new JTextField();
mpgTF = new JTextField();
dailycostTF = new JTextField();
//find button
calculateB = new JButton("Find");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//exit button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
Container pane = getContentPane();
pane.setLayout(new GridLayout(8, 4));
//panes
pane.add(firstNameL);
pane.add(firstNameTF);
pane.add(lastNameL);
pane.add(lastNameTF);
pane.add(milesL);
pane.add(milesTF);
pane.add(costL);
pane.add(costTF);
pane.add(mpgL);
pane.add(mpgTF);
pane.add(dailycostL);
pane.add(dailycostTF);
pane.add(calculateB);
pane.add(exitB);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
//find button
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//variables
double first, last, total, cost, averagempg, dailycost;
//strings to doubles
total = Double.parseDouble(milesTF.getText());
cost = Double.parseDouble(costTF.getText());
averagempg = Double.parseDouble(mpgTF.getText());
//calculates cost
dailycost = (total * cost)/averagempg;
//outputs text
dailycostTF.setText("$" + dailycost);
}
}
//exit button
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public static void main(String[] args){
Assignment2Part2 rectObject = new Assignment2Part2();
}
}
the output line being
dailycostTF.setText("$" + dailycost);
Any help would be great! I am completely new to Java.
An easy way is to use either the number format or decimal format class.
NumberFormat dollars = new NumberFormat.getCurrencyInstance();
Then you can format numbers into dollars quite easily. No clunky "$" needed.
DecimalFormat df = new DecimalFormat("#.##");
dailyCost = Double.valueOf(df.format(dailyCost));

ActionListener in GUI. Functionality doesn't work

I've got a question on my program, the program contains one single class Calculator, which should implement a calculator who is able to operate with + and * with the type double.
I have written a GUI for that calculator, too, which already works fine, but the buttons don't work, although I've implemented the function
public void actionPerformed(ActionEvent e)
The mistake has to be somewhere in this function I guess. I just don't know why the functionality of the buttons doesn't work. Here's the code.
public class Calculator extends JFrame implements ActionListener {
Calculator () {}
JTextField parameter1;
JTextField parameter2;
JTextField ergebnis;
JFrame window;
Container cont;
/* this function works fine */
public void calculator_GUI() {
builds the GUI of the calculator,
this.window = new JFrame("Calculator");
window.setSize(300,300);
this.parameter1 = new JTextField("Parameter1...", 10);
parameter1.addActionListener(this);
this.parameter2 = new JTextField("Parameter1...", 10);
parameter2.addActionListener(this);
this.ergebnis = new JTextField("Ergebnis...", 5);
ergebnis.addActionListener(this);
JButton multiplizieren = new JButton("*");
parameter1.addActionListener(this);
JButton addieren = new JButton("+");
parameter1.addActionListener(this);
JButton clear = new JButton("clear");
parameter1.addActionListener(this);
this.cont = window.getContentPane();
FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);
cont.setLayout(flowLayout);
cont.add(parameter1);
cont.add(parameter2);
cont.add(ergebnis);
cont.add(multiplizieren);
cont.add(addieren);
cont.add(clear);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);;
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("*")) {
String a = parameter1.getText();
String b = parameter2.getText();
double zahl1=Double.parseDouble(a);
double zahl2=Double.parseDouble(b);
double result = zahl1*zahl2;
String c = String.valueOf(result);
ergebnis.setText(c);
}
else if (label.equals("+")) {
String a = parameter1.getText();
String b = parameter2.getText();
double zahl1=Double.parseDouble(a);
double zahl2=Double.parseDouble(b);
double result = zahl1+zahl2;
String c = String.valueOf(result);
ergebnis.setText(c);
}
else if (label.equals("clear")) {
String z = "";
ergebnis.setText(z);
}
else {
window.dispose();
}
}
public static void main (String[] args) {
Calculator MyCalculator = new Calculator();
MyCalculator.calculator_GUI();
}
}
Looks like you have a copy-paste error:
This:
JButton multiplizieren = new JButton("*");
parameter1.addActionListener(this);
JButton addieren = new JButton("+");
parameter1.addActionListener(this);
JButton clear = new JButton("clear");
parameter1.addActionListener(this);
Should be:
JButton multiplizieren = new JButton("*");
multiplizieren.addActionListener(this);
JButton addieren = new JButton("+");
addieren.addActionListener(this);
JButton clear = new JButton("clear");
clear.addActionListener(this);
The problem is what #kuporific says but instead of doing implements ActionListener in your top level container you can:
create private classes, or
use anonymous classes
Example using Swing Action (anonymous classes)
JButton multiplizieren = new JButton("*");
multiplizieren.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
String a = parameter1.getText();
String b = parameter2.getText();
double zahl1=Double.parseDouble(a); // this can cause NumberFormatException
double zahl2=Double.parseDouble(b); // this can cause NumberFormatException
double result = zahl1*zahl2;
String c = String.valueOf(result);
ergebnis.setText(c);
}
});
JButton addieren = new JButton("+");
addieren.addActionListener((new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt){
String a = parameter1.getText();
String b = parameter2.getText();
double zahl1=Double.parseDouble(a);
double zahl2=Double.parseDouble(b);
double result = zahl1+zahl2;
String c = String.valueOf(result);
ergebnis.setText(c);
}
});
So instead of using ìf-else everywhere you can isolate associated actions for each button.
Besides you can add in the textfield a documentFilter that only accept numbers, or use a JFormattedTextField
Add actionListener to button instead of textfield

Using JTextField for user input

Thanks for your help guys...now the program works and runs like it should.. but I have 2 more question.
1.How can I get the output into a JTestField t4 or t5
2.How can I close the application using the JButton Buton3
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TriangleFarfan{
JFrame Triangle = new JFrame("Triangle Calculator");
JButton Button1 = new JButton ("Area");
JButton Button2 = new JButton("Perimeter");
JButton Button3 = new JButton("Close");
JTextField t1 = new JTextField(20);
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
JPanel j1 = new JPanel (new FlowLayout());
JLabel l1 = new JLabel("Enter side 1:");
JTextField t2 = new JTextField();
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
JPanel j2 = new JPanel (new FlowLayout());
JLabel l2 = new JLabel("Enter side 2:");
JTextField t3 = new JTextField();
String t3TextBox = t3.getText();
double side3 = Double.parseDouble(t3TextBox);
JPanel j3 = new JPanel (new FlowLayout());
JLabel l3 = new JLabel("Enter side 3:");
JTextField t4 = new JTextField();
JPanel j4 = new JPanel (new FlowLayout());
JLabel l4 = new JLabel("Area Result");
JTextField t5 = new JTextField(20);
JPanel j5 = new JPanel (new FlowLayout());
JLabel l5 = new JLabel("Perimeter Result");
public TriangleFarfan()
{
j1.add(l1);
j1.add(t1);
j2.add(l2);
j2.add(t2);
j3.add(l3);
j3.add(t3);
j4.add(l4);
j4.add(t4);
j5.add(l5);
j5.add(t5);
Triangle.add(j1);
Triangle.add(j2);
Triangle.add(j3);
Triangle.add(j4);
Triangle.add(j5);
Triangle.add(Button1);
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
});
Triangle.add(Button2);
Button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the Perimeter Button");
}
});
Triangle.add(Button3);
Button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
System.out.println("You clicked the Close Button");
}
});
Triangle.setLayout(new FlowLayout());
Triangle.setSize(450,400);
Triangle.setVisible(true);
Triangle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
In addition to missing a main method, as Reimeus pointed out, your order of instructions is wrong. You are trying to read the user input before anything is even shown on the screen, and even before an object is created. For example, this line:
String t1TextBox = t1.getText();
tries to obtain a text from a TextBox that wasn't even added to a Panel that wasn't yet created.
To solve this, you need to rethink the logic of your program. Here are a few hints:
avoid assignments outside methods. Instead of writing
JFrame Triangle = new JFrame("Triangle Calculator");
declare the variable in the class body like this:
JFrame Triangle;
and assign it inside the constructor like this:
Triangle = new JFrame("Triangle Calculator");
Build the whole UI, then worry about listeners. This way you can be sure that you are not referencing an UI element that does not exist when getting the user input.
Get the user input inside the listeners, like this:
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// get the size of side1 from the textbox
String t1TextBox = t1.getText();
double side1 = Double.parseDouble(t1TextBox);
// get the size of side2 from the textbox
String t2TextBox = t2.getText();
double side2 = Double.parseDouble(t2TextBox);
// now we can calculate the area
double Area = (side1 * side2)/2;
//Execute when button is pressed
System.out.println(Area);
}
});
Add a main method:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TriangleFarfan();
}
});
}
The declaration
JTextField t1 = new JTextField(20);
doesn't set the value in the JTextField to 20. Instead it sets the number of columns for the JTextComponent but with an empty String. Therefore the line
double side1 = Double.parseDouble(t1TextBox);
will throw an NumberFormatException on startup.

Categories

Resources