This discussion is archived
1 Reply Latest reply: Jan 13, 2013 12:54 AM by PhHein RSS

Input/String Help?

984458 Newbie
Currently Being Moderated
import java.util.Scanner;
class bc {
     public static void main(String[] args) {
          Scanner o = new Scanner(System.in);
          String a;
               System.out.print("Input Something: ");
               a = o.nextLine();
               
               if(a == "hey") {
                    Back();
               }else{
                    System.out.println("Invalid");
               }
     }
     public static void Back(){
          System.out.println("Hey, there.");
     }
}

How do I make this properly work where if the user inputs "hey", it will print out the method Back, instead of just "Invalid"?

P.S. How do I make my Java code in format for the forums?
  • 1. Re: Input/String Help?
    PhHein Guru Moderator
    Currently Being Moderated
    Formatting code is dscribed in the announcements: https://forums.oracle.com/forums/ann.jspa?annID=1389
    Please search for Java coding conventions, especially what is capitalized and what not.
    Strings are never compared with == , that's why you get unexpected results. I suggest you look at the tutorials.

    Moderator action: moving this thread to NTJ.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points