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

Thread: Boolean or If?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Boolean or If?

    After a few tutorials I've finally started playing around with the GUI and I'm playing with taking user input and putting them into an equation. Right now my problem is trying give a Yes/No answer a value. At first I tried to make it a boolean but got utterly confused. So I turned it into a string and made an if statement that changes the value of a variable. The issue I am having right now is that whether or not I put yes or no for "Are you furthernig your education?" I will get the same output value.

    package sandbox;
     
    import javax.swing.JOptionPane;
     
    class Play {
    	public static void main(String[] args){
    		String money =JOptionPane.showInputDialog("How often do you get paid in a week?");
    		String edu =JOptionPane.showInputDialog("Are you furthering your education?");
    		String pt =JOptionPane.showInputDialog("How often do you workout in a week?");
     
    		int school;
     
    		if (edu == "Yes"){
    			school = 5;
    		} else {school = 0;}
     
    		int paid = Integer.parseInt(money);
    		int excercise = Integer.parseInt(pt);
    		int score = paid+excercise*3*school/2;
     
    		JOptionPane.showMessageDialog(null, "Your value is " + score, "Pass!", JOptionPane.PLAIN_MESSAGE);
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Boolean or If?

    See: Common Java Mistakes: == operator or equals() method

    After reading that, take a closer look at this line of code:

    if (edu == "Yes")

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

    0ffConstantly (October 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Boolean or If?

    Ahhh okay. That was simple. If any other beginners out there are thinking about trying codeacademy.com just don't. They have me confused and using ridiculously wrong syntax.
    Here's the code I ended up with.
    package sandbox;
     
    import javax.swing.JOptionPane;
     
    class Play {
    	public static void main(String[] args){
    		String money =JOptionPane.showInputDialog("How often do you get paid in a week?");
    		String edu =JOptionPane.showInputDialog("Are you furthering your education?");
    		String pt =JOptionPane.showInputDialog("How often do you workout in a week?");
     
    		int school;
    		String eduAnswer = "Yes";
     
    		if (edu.equals(eduAnswer)){
    			school = 5;
    		} else {school = 0;}
     
    		int paid = Integer.parseInt(money);
    		int excercise = Integer.parseInt(pt);
    		int score = paid+excercise*3*school/2;
     
    		JOptionPane.showMessageDialog(null, "Your value is " + score, "Pass!", JOptionPane.PLAIN_MESSAGE);
    	}
    }

    Thanks!

  5. #4
    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: Boolean or If?

    Just so it was mentioned also... .equalsIgnoreCase

    ...and if your question has been answered please mark the thread as solved. Assistance here if needed.

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

    0ffConstantly (October 26th, 2012)

Similar Threads

  1. What does '^' do? Is this a boolean?
    By ColeTrain in forum Java Theory & Questions
    Replies: 2
    Last Post: October 19th, 2012, 02:58 PM
  2. [SOLVED] What's wrong with this boolean?
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 8th, 2011, 06:41 PM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Some help with boolean
    By JPetroSS in forum Java Theory & Questions
    Replies: 3
    Last Post: November 2nd, 2010, 05:38 AM
  5. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM