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: (beginner) if else code.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default (beginner) if else code.

    i cant get this code to do the if else part of the code :/ can you guys help me out

    thanks!!


    	public class HelloWorld
    	{
    		public static void main(String[] args)
    	{
     
     
     
    		Scanner input = new Scanner(System.in);
    		//this asks the user if they want to times or add or minus.
    		System.out.print("what you like to do? ");
    		System.out.print("would you like to add, times, or minus?");
    		//this gets the users inputs and assign's it to question.
    		String add = "add";
    		String times = "times";
    		String minus = "minus";
     
    		String question = input.next();
     
     
     
    		if (question == add) {
    		    System.out.println ((times));
    		} else {
    		    System.out.println((minus));


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: (beginner) if else code.

    Define "cant get this code to do the if else part"...and I recommend using equals instead of == when comparing Strings. See
    Common Java Mistakes

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (beginner) if else code.

    when I say "I cant", I mean it always goes to the else statement...... And thank you i got it working!!!!!!!!!
    Last edited by dumb1101; January 15th, 2012 at 05:25 PM.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: (beginner) if else code.

    Quote Originally Posted by dumb1101 View Post
    when I say "I cant", I mean it always goes to the else statement...... And thank you i got it working!!!!!!!!!
    It's good that you've solved your problem. Don't forget to read the difference between == and equals()

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    107
    My Mood
    Grumpy
    Thanks
    6
    Thanked 2 Times in 1 Post

    Default Re: (beginner) if else code.

    Quote Originally Posted by dumb1101 View Post
    i cant get this code to do the if else part of the code :/ can you guys help me out

    thanks!!


    	public class HelloWorld
    	{
    		public static void main(String[] args)
    	{
     
     
     
    		Scanner input = new Scanner(System.in);
    		//this asks the user if they want to times or add or minus.
    		System.out.print("what you like to do? ");
    		System.out.print("would you like to add, times, or minus?");
    		//this gets the users inputs and assign's it to question.
    		String add = "add";
    		String times = "times";
    		String minus = "minus";
     
    		String question = input.next();
     
     
     
    		if (question == add) {
    		    System.out.println ((times));
    		} else {
    		    System.out.println((minus));

    	            	if  (question == add) 
    	            		System.out.println (times)
    	            	else
    	            		System.out.println(minus);

Similar Threads

  1. [SOLVED] What's wrong with my code?? (Total beginner)
    By TheProf in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 6th, 2012, 02:41 PM
  2. Please held. What is wrong with my code? I am a beginner ;)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 30th, 2011, 05:33 PM
  3. Beginner pleasee help with java code
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 07:38 PM
  4. ServletExample code problem - beginner
    By Sawubona in forum Java Servlet
    Replies: 5
    Last Post: July 4th, 2011, 06:57 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 10:30 AM