Here is my code so far: import java.awt.EventQueue; import javax.swing.JFrame; i
ID: 666509 • Letter: H
Question
Here is my code so far:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//Brittany Lasher
//Right now it only works if you enter in all 6 values, I'm now sure why yet. I will continue to work on it and figure it out
public class Window {
private JFrame frmBrittanysWindow;
private JTextField nmb3;
private JTextArea textArea;
private JTextField nmb1;
private JLabel lblNumbers;
private JTextField nmb2;
private JTextField nmb4;
private JTextField nmb5;
private JTextField nmb6;
private JLabel lblMaximum;
private JLabel lblMinimum;
private JLabel lblAverage;
private JLabel lblNewLabel;
private JButton btnC;
private JButton btnE;
private JTextField Min;
private JTextField Max;
private JTextField Av;
private JTextField How;
double d1=0;
double d2=0;
double d3=0;
double d4=0;
double d5=0;
double d6=0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frmBrittanysWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Window() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmBrittanysWindow = new JFrame();
frmBrittanysWindow.setTitle("Brittany's Window");
frmBrittanysWindow.setBounds(100, 100, 450, 300);
frmBrittanysWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmBrittanysWindow.getContentPane().setLayout(null);
nmb3 = new JTextField();
nmb3.setBackground(Color.WHITE);
nmb3.setBounds(16, 120, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb3);
nmb3.setColumns(10);
textArea = new JTextArea();
textArea.setBounds(63, 92, 1, 16);
frmBrittanysWindow.getContentPane().add(textArea);
nmb1 = new JTextField();
nmb1.setBounds(16, 59, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb1);
nmb1.setColumns(10);
lblNumbers = new JLabel("Numbers");
lblNumbers.setBounds(34, 28, 61, 16);
frmBrittanysWindow.getContentPane().add(lblNumbers);
nmb2 = new JTextField();
nmb2.setBounds(16, 92, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb2);
nmb2.setColumns(10);
nmb4 = new JTextField();
nmb4.setBounds(16, 160, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb4);
nmb4.setColumns(10);
nmb5 = new JTextField();
nmb5.setBounds(16, 200, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb5);
nmb5.setColumns(10);
nmb6 = new JTextField();
nmb6.setBounds(16, 240, 134, 28);
frmBrittanysWindow.getContentPane().add(nmb6);
nmb6.setColumns(10);
lblMaximum = new JLabel("Maximum");
lblMaximum.setBounds(200, 59, 73, 22);
frmBrittanysWindow.getContentPane().add(lblMaximum);
lblMinimum = new JLabel("Minimum:");
lblMinimum.setBounds(200, 92, 73, 16);
frmBrittanysWindow.getContentPane().add(lblMinimum);
lblAverage = new JLabel("Average:");
lblAverage.setBounds(200, 126, 61, 16);
frmBrittanysWindow.getContentPane().add(lblAverage);
lblNewLabel = new JLabel("How Many:");
lblNewLabel.setBounds(200, 160, 82, 22);
frmBrittanysWindow.getContentPane().add(lblNewLabel);
btnC = new JButton("Calculate");
btnC.addActionListener(new BtnCActionListener());
btnC.setBounds(156, 201, 117, 29);
frmBrittanysWindow.getContentPane().add(btnC);
btnE = new JButton("Exit");
btnE.addActionListener(new BtnEActionListener());
btnE.setBounds(287, 201, 117, 29);
frmBrittanysWindow.getContentPane().add(btnE);
Min = new JTextField();
Min.setEditable(false);
Min.setBounds(270, 86, 134, 28);
frmBrittanysWindow.getContentPane().add(Min);
Min.setColumns(10);
Max = new JTextField();
Max.setEditable(false);
Max.setBounds(270, 59, 134, 28);
frmBrittanysWindow.getContentPane().add(Max);
Max.setColumns(10);
Av = new JTextField();
Av.setEditable(false);
Av.setBounds(258, 120, 134, 28);
frmBrittanysWindow.getContentPane().add(Av);
Av.setColumns(10);
How = new JTextField();
How.setEditable(false);
How.setBounds(270, 160, 134, 28);
frmBrittanysWindow.getContentPane().add(How);
How.setColumns(10);
}
private class BtnCActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int a=1;
int b=1;
int c=1;
int d=1;
int f=1;
int g=1;
if(!nmb1.getText().isEmpty()){
String a1 =nmb1.getText();
d1=Double.parseDouble(a1);}
if(!nmb2.getText().isEmpty()){
String a2 =nmb2.getText();
d2=Double.parseDouble(a2);}
if(!nmb3.getText().isEmpty()){
String a3 =nmb3.getText();
d3=Double.parseDouble(a3);}
if(!nmb4.getText().isEmpty()){
String a4 =nmb4.getText();
d4=Double.parseDouble(a4);}
if(!nmb5.getText().isEmpty()){
String a5 =nmb5.getText();
d5=Double.parseDouble(a5);}
if(!nmb6.getText().isEmpty()){
String a6 =nmb6.getText();
d6=Double.parseDouble(a6);}
System.out.println(d1+d2+d3+d4+d5+d6);
//Av.setText(""+(d1+d2+d3+d4+d5+d6)/6);
if( d1>d2 && d1>d3 && d1>d4 && d1>d5 && d1>d6){
Max.setText(""+d1);
}
else if( d2>d1 && d2>d3 && d2>d4 && d2>d5 && d2>d6){
Max.setText(""+d2);
}
else if(d3>d1 && d3>d2 && d3>d4 && d3>d5 && d3>d6){
Max.setText(""+d3);
}
else if(d4>d1 && d4>d2 && d4>d3 && d4>d5 && d4>d6){
Max.setText(""+d4);
}
else if(d5>d1 && d5>d2 && d5>d4 && d5>d3 && d5>d6){
Max.setText(""+d5);
}
else if(d6>d1 && d6>d2 && d6>d4 && d6>d5 && d6>d3){
Max.setText(""+d3);
}
if( d1 Min.setText(""+d1);
}
else if( d2 Min.setText(""+d2);
}
else if(d3 Min.setText(""+d3);
}
else if(d4 Min.setText(""+d4);
}
else if(d5 Min.setText(""+d5);
}
else if(d6 Min.setText(""+d3);
}
String b1 =How.getText();
double b2=Double.parseDouble(b1);
How.setText(""+(g+f+a+b+c+d));
Av.setText(""+(d1+d2+d3+d4+d5+d6)/b2);
//How.setText(""+a1+a2+a3+a4+a5+a6);
//Av.setText(""+(d1+d2+d3+d4+d5+d6)/6);//I will divide it by how many, once I get how many working
}
}
private class BtnEActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
Explanation / Answer
if( d1 Min.setText(""+d1);
}
THE ABOVE STATEMENT IS THE CAUSE OF YOUR ERROR. AN ELSE-IF LADDER ENDS IN AN ELSE BLOCK INSTEAD WHAT YOU SEEM TO HAVE DONE IS START ANOTHER ELSE-IF LADDER WITH PROPERLY ENDING THE FIRST ONE. ALSO THERE IS AN ERROR IN THE IF CONDITION OF THE SECOND ELSE-IF LADDER. IF YOU RECTIFY BOTH THESE THINGS YOUR PROGRAM WOULD WORK.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.