|
-
March 8th, 2012, 05:36 PM
#1
Java If Statement
Hi Everyone,
I am developing a java application and i was wondering if anyone could help me.
I was trying to do an if statement where if i click on the view report button it checks the value if text6 is below 40, then it will display on text7 Fail. And if the value is above 40, then it will display on text7 as pass.
Here is the code below:
Please help me
Code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class Please extends JFrame implements ActionListener
{
JPanel panel;
JButton View = new JButton("View Report");
JLabel label1 = new JLabel("Student ID");
JLabel label2 = new JLabel("Name");
JLabel label3 = new JLabel("Surname");
JLabel label4 = new JLabel("Coursework %");
JLabel label5 = new JLabel("Exam %");
JLabel label6 = new JLabel("Overall Mark");
JLabel label7 = new JLabel("Grade");
JTextField text1 = new JTextField("007");
JTextField text2 = new JTextField("James");
JTextField text3 = new JTextField("Bond");
JTextField text4 = new JTextField("40");
JTextField text5 = new JTextField("50");
JTextField text6 = new JTextField("");
JTextField text7 = new JTextField("")
private JMenuBar bar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenu editMenu = new JMenu("Edit");
private JMenuItem reallyQuitChoice = new JMenuItem("Quit");
private JMenuItem modifyChoice = new JMenuItem("Modify");
public Please()
{
setTitle("Records");
bar.add(fileMenu);
bar.add(editMenu);
fileMenu.add(reallyQuitChoice);
editMenu.add(modifyChoice);
setJMenuBar(bar);
reallyQuitChoice.addActionListener(this);
modifyChoice.addActionListener(this);
panel = new JPanel(new GridLayout(7,1));
panel.add(label1);
panel.add(text1);
text1.setEditable(false);
panel.add(label2);
panel.add(text2);
text2.setEditable(false);
panel.add(label3);
panel.add(text3);
text3.setEditable(false);
panel.add(label4);
panel.add(text4);
text4.setEditable(false);
panel.add(label5);
panel.add(text5);
text5.setEditable(false);
panel.add(label6);
panel.add(text6);
text6.setEditable(false);
panel.add(label7);
panel.add(text7);
text7.setEditable(false);
add(panel,BorderLayout.CENTER);
add(View,BorderLayout.SOUTH);
View.addActionListener(this);
}
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == View)
{
int one=Integer.parseInt(text4.getText());
int two=Integer.parseInt(text5.getText());
text6.setText(""+((one+two)/2));
}
if(e.getSource() == reallyQuitChoice)
{
System.exit(0);
}
if(e.getSource() == modifyChoice)
{
text4.setEditable(true);
text5.setEditable(true);
}
}
public static void main(String[] args)
{
new Please();
}
}
-
March 9th, 2012, 11:09 AM
#2
Re: Java If Statement
Please use sensible names for variables and use the Java naming conventions (ie variable names should start with lower case letters). So 'View' should be something like 'viewBtn' or 'viewReportBtn'.
Also it's better to add a listener to the component that will generate the event rather than having all components events going to the same listener and then having to sort out where the event came from etc. ie:
Code:
viewBtn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// add your handling code for view report events here
}
});
I was trying to do an if statement where if i click on the view report button it checks the value if text6 is below 40, then it will display on text7 Fail.
so why is your code for this event getting the values from text4 and text5 and printing the average value in text6?
-
March 10th, 2012, 06:57 AM
#3
Re: Java If Statement
Indeed having sensible and readable names for variable, method and class is essential for any commercial java program. check this if helps http://codebuild.blogspot.com/2012/0...le-method.html
-
March 12th, 2012, 03:37 AM
#4
Re: Java If Statement
Something like this should work:
Code:
if (e.getSource() == View) {
int one = Integer.parseInt(text4.getText());
int two = Integer.parseInt(text5.getText());
text6.setText("" + ((one + two) / 2));
if (Integer.parseInt(text6.getText()) >= 40) {
text7.setText("fail");
}
else {
text7.setText("pass");
}
}
If this is homework, forget what I'm going to say.
However, I don't understand why people try to learn first AWT or Swing before learning how language works. How if/else works, how parse a value or convert to or from String. Such things are much more basic than learn a GUI library. And, of course, follow Java naming conventions. Just my opinion.
Regards,
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
-
March 12th, 2012, 09:13 AM
#5
Re: Java If Statement
Something like this should work:
Why are you converting the int value to a String, setting the value in text6 and then on the next line getting the value back out of text6 and converting it back to an integer so you can compare it to 40? Why not save the calculated value in a temporary int variable and use the temporary int variable to set the value in text6 and to do the comparison with 40.
-
March 12th, 2012, 12:11 PM
#6
Re: Java If Statement
I just added what I understood he/she attempted to do:
checks the value if text6 is below 40, then it will display on text7 Fail. And if the value is above 40, then it will display on text7 as pass.
Nevertheless, you are right. I should have corrected whole code. My mistake. Sorry
Albert.
Please, correct me. I'm just learning.... and sorry for my english :-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|