0

Possible Duplicate:
How do I compare strings in Java?

yes the title sounds funny, I am working on this project for a special someone, and I want to ask her out by writing her a simple program. I am having some trouble though with the execution of this idea. I have a pseudo code ish plan in my mind but need help executing it. I was planning on having her input yes or no answers to questions in the console and have the program respond with answers based on her inputs. my Idea was her input yes or no answers to questions, and then say something like If input is "yes" then print this statement, else if "no" print this statement. I am currently having problems getting the if else part to work. I cant have strings act like booleans in an if statment, so I need some help with that. code I started with:

import java.io.*;
public class ask {
    public static void main(String [] args) throws IOException {
        BufferedReader in = new BufferedReader
                (new InputStreamReader(System.in));
        String answer;
        System.out.print("Would you like to go out with me? ");
        System.out.flush();
        answer = in.readLine();
        Boolean hope = Boolean.valueOf("yes");
        if (answer == hope) {
            System.out.print("Awesome!");
        }

        else
            System.out.print("Not awesome!");

    }
}

obviously Im not actually using that question those responses, im just putting it as in example just to get the program itself to work. I cannot compare answer to hope because one is boolean and the other is a string, I also cannot do string with string, so how do I get this to work. Any ideas? Edit: Guys just to say, I know she will say yes, this is just a funny silly way to ask her out. I will be there IN PERSON with her while doing this, and I will tell her only to use yes or no so that way this works, its just a small simple project. Don't need any advice or opinion on anything other than how to get this to work please! Thanks!

Community
  • 1
  • 1
user12074577
  • 1,371
  • 8
  • 25
  • 30
  • 11
    Piece of advice. Don't. She'll most likely think you are weird / a creep. Ask her in person. – Stephen C Jan 13 '13 at 06:23
  • how does that help it? I need it to check if the input is yes or no – user12074577 Jan 13 '13 at 06:25
  • I think you should listen to keys she types[not just the final input] while thinking at the time of replying as well – jmj Jan 13 '13 at 06:25
  • lol @stephen dont worry man shes going to say yes and im showing her this in person, its just a goofy way to ask her out. She will say yes. This is for fun not serious. – user12074577 Jan 13 '13 at 06:26
  • 1
    You're really asking this and everything else is just unneeded information: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – djechlin Jan 13 '13 at 06:26
  • 1
    So you plan to make her follow you into a room with a computer then run this program and then make her type things on the keyboard? It'd be easier to ask her out in person. And what if she types in y or misspells 'yes'? – Sanchit Jan 13 '13 at 06:26

4 Answers4

3

you can use string.equals() to compare string E.g. "Yes".equals(answer) for comparison of yes and the variable answer

Hitman47
  • 421
  • 4
  • 8
2

You can use String Comparison methods equals or equalsIgnoreCase to compare the user input.

Try this

public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String answer;
        System.out.print("Would you like to go out with me? ");
        System.out.flush();
        answer = in.readLine();

        if (answer.equalsIgnoreCase("YES")) { // Comparing the input with String.
            System.out.print("Awesome!");
        }

        else
            System.out.print("Not awesome!");

    }

I suggest you to use equalsIgnoreCase method. Since the user can input yes, YES, yeS etc. It just ignores the case.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

Investigate the switch statement: Info from the tutorial

You could then throw in options like "maybe", "not right now", etc as possible responses in addition to just yes & no.

mcalex
  • 6,628
  • 5
  • 50
  • 80
  • 1
    from that tutorial page: 'It also works with enumerated types (discussed in Enum Types), **the String class**, and a few special classes that wrap certain primitive ...'. I didn't know OP was referring to old java – mcalex Jan 13 '13 at 06:33
  • Note: this only works in Java 1.7+ – John Dvorak Jan 13 '13 at 06:36
0

Here You go : Just Replace array values with your specific Questions

import java.io.*;
import java.util.Scanner;
public class ask {
public static void main(String [] args) throws IOException {

    String[] Questions={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

    int min=0;
    int max=Questions.length;
    String answer = null;
    Scanner input = new Scanner(System.in);
    for(int i=0;i<Questions.length;i++){
         int randomNum = min + (int)(Math.random()* max);

         System.out.println("Your Question: " +Questions[randomNum] ); 
         answer =input.next();
         if (answer.equals("yes")){
             System.out.println("Awesome Girl");
         }
         if (answer.equals("no")){
             System.out.println("Bad Girl");
         }

         if (answer.equals("Hope")){
             System.out.println("Hmmm..Some Hope Left");
         }


         } } }
Deathstar
  • 75
  • 5
  • 13