Results 1 to 4 of 4
  1. #1
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Very Simple Program But Can't Get Right

    Hello,

    I am just learning classes and stuff. But I am just making the first class. It won't seem to work what I'm trying to do.


    import java.util.Scanner;


    public class BuildComputer {
    public static void main (String[]args)
    {
    String Y = "Y";
    String N = "N";
    String buildquestion;
    Scanner scan = new Scanner (System.in);

    System.out.println("Are you looking to build your computer? Y for Yes & N for No");

    buildquestion = scan.nextLine();

    System.out.println("You answered:" + buildquestion );

    if(buildquestion == Y)
    {
    System.out.println("Sounds good! Lets get started!");
    }
    else
    System.out.println("Maybe another time");


    }

    }


    Every time I input "Y" it just prints out the else statement. I am very new with the if else stuff. But what is wrong here. Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Very Simple Program But Can't Get Right

    One problem with your code is that you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    Java Code:
    if (fu == "bar") {
      // do something
    }
    do,

    Java Code:
    if (fu.equals("bar")) {
      // do something
    }
    or,

    Java Code:
    if (fu.equalsIgnoreCase("bar")) {
      // do something
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Very Simple Program But Can't Get Right

    Google "java string comparison"

  4. #4
    mike_ is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Very Simple Program But Can't Get Right

    thank you! it fixed it. little things like that always seem to mess me up.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  2. simple program
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 05-01-2011, 10:03 PM
  3. Simple program
    By 479320 in forum New To Java
    Replies: 6
    Last Post: 04-08-2011, 08:27 AM
  4. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM
  5. Simple Program
    By TheRocket in forum Advanced Java
    Replies: 15
    Last Post: 12-30-2008, 02:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •