Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java If Statement

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java If Statement

    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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    thretz (March 8th, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  6. The Following User Says Thank You to Norm For This Useful Post:

    thretz (March 8th, 2012)

  7. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java If Statement

    i am not very good at if statements.
    is it possible if you could give me an example please.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  9. The Following User Says Thank You to Norm For This Useful Post:

    thretz (March 8th, 2012)

  10. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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");
    }

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  12. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java If Statement

    Everything works
    thanks again

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java If Statement

    Glad you got it working.

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. not a statement?
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 23rd, 2011, 08:57 PM
  3. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2010, 12:31 AM
  4. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM
  5. How to Use the Java switch statement
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: October 8th, 2009, 09:00 PM