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: How do I run the if statement?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb How do I run the if statement?

    Hi guys, I'm new to Java programming and I ran into a problem that I don't know how to solve. I'd appreciate it if anyone could help me.

    So, I'm trying to run an if statement with an input condition and here is my code.

    import java.util.Scanner;
    public class App10A {
    public static void main(String[] args) {
    Scanner fun = new Scanner(System.in);
    System.out.println("What's your name?");
    String name = fun.nextLine();
    if (name == "Victor") {
    System.out.println("This is the result I wanted!");
    }
    else {
    System.out.println("Oops, try again!");
    }
    }
    }

    When the computer asked me "What's your name?", I typed in Victor, then the result turned out to be "Oops, try again!".
    I typed in Victor (without parenthesis) and "Victor" and both gave me the negative results.
    What exactly should I put in in order to get "This is the result I wanted!" ?
    Or, how should I modify my code in order to get "This is the result I wanted!" when I put in Victor (without changing the equal sign)?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I run the if statement?

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please post your code correctly.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: How do I run the if statement?

    You are trying to compare a string with the compare method of two integers , if you had like:
    int A =1 ;
    int B=2;
    here you can use if(A==B)
    return true;
    However when you want to compare Strings you use the .equalsTo , as an example:
    String A = test;
    String B = Java;
    if(a.equalsTo(B))
    return true;

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I run the if statement?

    Thank you so much!

  5. #5
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: How do I run the if statement?

    Note: The String class does not have an "equalsTo" method. It has an "equals" method. It's not enough know how to do it, you also need to know why. See Java: String Comparison and Java: ==, .equals(), compareTo(), and compare().

Similar Threads

  1. [SOLVED] Java 1.4.2 and eclipse code will not compile or run need it to run for school!
    By ClarmonkGaming in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 23rd, 2014, 09:41 PM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 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. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM

Tags for this Thread