-
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
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();
}
}
-
Re: Java If Statement
Quote:
if text6 is below 40,
Where is your code to test the contents of text6?
You will need to get the contents of the textfield and convert it to an int value to be able to compare it to 40.
See the Integer class for a method to convert/parse a String to an int.
-
Re: Java If Statement
ok, so i should do something like
int three=Integer.parseInt(text6.getText());
if(three == <40);
I then get an error on the if statement line.
-
Re: Java If Statement
Look at the choice of operators that you can use to compare two numbers.
== is one
< is one
What are the other choices?
Do any of them sound like they would do what you want.
==< is NOT a valid operator for comparing two numbers
-
Re: Java If Statement
i am not very good at if statements.
is it possible if you could give me an example please.
-
Re: Java If Statement
if(<a boolean expression>) {
// do this if true
} // end if()
<a boolean expression> should be a boolean expression that evaluates to true or false.
Your text book should have examples of boolean expressions.
The basic one is: true
a more complex one: operand boolean-operator operand
See your text book for the full syntax and examples
-
Re: Java If Statement
thanks for your help
this is what i did
int three=Integer.parseInt(text6.getText());
if(three<40)
{
text7.setText("Fail");
}
else
{
text7.setText("Pass");
}
-
Re: Java If Statement
Does it compile, execute and do what you want? Then you've solved that problem.
One thing you will want to do some time is test the that the contents of text6 is valid.
You'll see how to do that later.
-
Re: Java If Statement
Everything works
thanks again
-
Re: Java If Statement