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

Thread: Compiler refuses to "accept" my string variable - need help

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Compiler refuses to "accept" my string variable - need help

    package input;

    import java.util.Scanner;

    public class Input {

    /**
    * Created by Björn Johansson 2012
    */

    public static void main(String[] args)
    {

    Scanner user_input = new Scanner(System.in);

    String first_name;
    System.out.print("Vad heter du i förnamn? ");
    first_name = user_input.next();

    if (first_name = "Ola")
    {
    System.out.print("Hej Ola!");
    }

    else if (first_name = "Eva")
    {
    System.out.print("Hej Eva!");
    }

    else
    {
    System.out.print("Du har angett ett ogiltigt namn!");
    }


    }

    }


    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Type mismatch: cannot convert from String to boolean
    Type mismatch: cannot convert from String to boolean

    at input.Input.main(Input.java:20)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Compiler refuses to "accept" my string variable - need help

    cannot convert from String to boolean
    The compiler wants a boolean value, not a String in the if statement.
    The if statement requires a boolean value. The assignment statement (=) returns the value of the token to the left of the = (a String). If you use the == operator, the result will be a boolean value that is the results of the comparison.

    To compare the contents of two Strings you should use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Compiler refuses to "accept" my string variable - need help

    I replaced
    first_name =
    with
    first_name ==
    now the program compiles and runs without problems! But when I enter the name Eva as a name, I get as a result ogiltigt namn! (that is disqualified name in english) but it should return the text: Hej Eva!

    So how do I fix that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Compiler refuses to "accept" my string variable - need help

    To compare the contents of two Strings you should use the equals() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Compiler refuses to "accept" my string variable - need help

    I was reading over at java-samples.com for some examples of how to do this, but I must admit that since I'm a total newbie on java, I'm not sure what I'm doing at times, so can you give me a hint on how I should do that in source code?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Compiler refuses to "accept" my string variable - need help

    Are you asking how to use a class's method?

    refToClass.theMethod(theArgsForTheMethod);

    Read the API doc for the class to see what the method does, what its arguments are and what it returns.
    The API doc: Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    khelben1979 (November 27th, 2012)

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  2. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  3. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM