Why does this not work.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rscorpio64
    New Member
    • Oct 2008
    • 5

    Why does this not work.

    Bad thing about knowing a few languages, you have a tendacy to get them mixed up. Such is this case (I hope).
    Code:
    tmpStr = JOptionPane.showInputDialog("Would you like to select your \nown terminal velocity?[Y/n]: ");
    if (user_ans=="" || user_ans=="Y"|| user_ans=="y"){
    			user_ans="Y";
    		}else{user_ans="N";};
    why does this always produce a "N"

    its a yes/no question - basically if a user enters a y regardless of case or a empty (by just hitting enter) it will mean they entered yes, if anthing else, its no. whenever i put a y or a Y in it, it yeilds a N.
    whats wrong with this code?
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by rscorpio64
    Code:
    tmpStr = JOptionPane.showInputDialog("Would you like to select your \nown terminal velocity?[Y/n]: ");
    if (user_ans=="" || user_ans=="Y"|| user_ans=="y"){
    			user_ans="Y";
    		}else{user_ans="N";};
    why does this always produce a "N"
    Because you don't compare Strings like that in Java - use
    [CODE=java] if (user_ans.equal s("") || user_ans.equals ("Y")|| user_ans.equals ("y")){
    //...[/CODE] instead.

    By the way, I can understand the confusion with knowing a few languages. ^^

    Greetings,
    Nepomuk

    Comment

    • rscorpio64
      New Member
      • Oct 2008
      • 5

      #3
      omg I would have never figured that out - I must have been scouring the net for the wrong question LOL.

      tyvm!!!!!!!!!!! !!!

      Comment

      Working...