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

Thread: User input string's problem

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default User input string's problem

    this is my code:


    import java.util.Scanner;
    public class prac {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    System.out.print("input : ");
    String x = input.next();
    System.out.println(x=="a");



    }

    }

    I'm a beginner of Java,
    The code above is that simply lets the user enter a string type variable, and then compare whether the x variable is equal to "A", my problem is when i input "a" to x variable , but the result of System.out.println(x=="a") is false ! why ? can someone explain ?

  2. #2
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: User input string's problem

    String is object, == compares
    if two objects are exactly same, not
    if they have stored same values "a"
    use
    x.equals("a")
    for better result

Similar Threads

  1. [SOLVED] Determine if user input is an integer or String
    By CADman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 26th, 2021, 12:03 PM
  2. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 10:26 PM
  3. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  4. Parsing a string to an int while receiving user input?
    By happyxcrab in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 05:38 PM
  5. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM