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 5 of 5

Thread: the if statement does not work

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default the if statement does not work

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.text.DecimalFormat;
    import javax.swing.event.*;

    public class japanese extends JApplet {

    public void init() {

    JTextArea outputArea;
    outputArea = new JTextArea(2,10);

    String output = ("");
    JOptionPane.showMessageDialog(null,"uchi or ie ","house",
    JOptionPane.INFORMATION_MESSAGE);
    String house = JOptionPane.showInputDialog("enter the japanese word for " +
    "house");
    String h = new String("uchi");
    String h1 = new String("ie");
    output = ("good");
    outputArea.setText(output);
    if (house == h || house == h1)
    JOptionPane.showMessageDialog(null,outputArea,"hou se",
    JOptionPane.INFORMATION_MESSAGE);

    else
    JOptionPane.showMessageDialog(null,"not good","house",
    JOptionPane.INFORMATION_MESSAGE);
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: the if statement does not work

    Wrap your code in Code tags, ask a question (no, the title doesn't count). Don't compare Strings with ==.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: the if statement does not work

    if((*String*.equalsIgnoreCase(*String variable or String value*))||(*String*.equalsIgnoreCase(*String variable or String value*))){

    /*statement*/

    }

    else{

    /*statement*/


    }

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the if statement does not work

    Quote Originally Posted by PhHein View Post
    Wrap your code in Code tags, ask a question (no, the title doesn't count). Don't compare Strings with ==.
    thank you, it worked.

    Now I am having problems with the actionListener. How do I get it to work so that the volume takes all variables?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.awt.Container;

    public class rectangularSolid extends JApplet implements ActionListener {

    public void init() {

    String in = JOptionPane.showInputDialog("1(volume),2(surface area),3(diagonal)");
    int input = Integer.parseInt(in);

    switch(input) {

    case 1:
    JLabel prompt;
    JLabel prompt2;
    JLabel prompt3;
    JLabel prompt4;
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    JTextField insert;
    JTextField insert2;
    JTextField insert3;
    JTextField insert4;
    prompt = new JLabel("enter length");
    insert = new JTextField(10);
    c.add(prompt);
    c.add(insert);
    prompt2 = new JLabel("enter width");
    insert2 = new JTextField(10);
    c.add(prompt2);
    c.add(insert2);
    prompt3 = new JLabel("enter height");
    insert3 = new JTextField(10);
    c.add(prompt3);
    c.add(insert3);
    prompt4 = new JLabel("answer is ");
    insert4 = new JTextField(10);
    c.add(prompt4);
    c.add(insert4);
    insert4.addActionListener(this);
    break;
    case 2:
    JTextArea outputArea;
    outputArea = new JTextArea(2,10);
    Container c1 = getContentPane();
    c1.add(outputArea);
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    String sa = JOptionPane.showInputDialog("enter length");
    double length2 = Double.parseDouble(sa);
    String w2 = JOptionPane.showInputDialog("enter width");
    double width2 = Double.parseDouble(w2);
    String h2 = JOptionPane.showInputDialog("enter height");
    double height2 = Double.parseDouble(h2);
    String output = "";
    output = (" Surface area = "
    + twoDigits.format(surfaceArea(length2,width2,height 2)));
    outputArea.setText(output);
    JOptionPane.showMessageDialog(null,outputArea,null ,
    JOptionPane.INFORMATION_MESSAGE);
    break;}
    }
    public void actionPerformed(ActionEvent e ) {
    double length = Double.parseDouble(e.getActionCommand() );
    double width = Double.parseDouble(e.getActionCommand());
    double height = Double.parseDouble(e.getActionCommand());
    showStatus("Volume is " + volume ( length, width, height ) );
    }
    public double volume(double length, double width, double height) {
    return length * width * height;
    }
    public double surfaceArea(double length2, double width2, double height2) {
    return ( 2 * length2 * width2 + 2 * length2 * height2 + 2 * width2 * height2);

    }
    }

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: the if statement does not work

    Please consult the Announcements page for the use of code tags in your posts. If we spend all of our time repairing your post, we have no time left to answer questions.

Similar Threads

  1. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 1st, 2013, 11:16 PM
  2. Default does not work in Switch Statement
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 3
    Last Post: March 29th, 2013, 07:15 AM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. Can't get the Update statement to work
    By reptar693 in forum JDBC & Databases
    Replies: 1
    Last Post: March 16th, 2012, 02:03 AM