0

"if" statement only allows to put numbers in it. Is there a way to make it read letters? I'm only in my fifth lesson of Java (I study in a uni and the teacher is very slow but I want to learn things fast)

for example.

import java.util.Scanner;
public class Java {
    public static void main (String [] args) {

        Scanner scan = new Scanner(System.in);
        int answer1;
        System.out.println("Do you like Java?");
        answer1 = scan.nextInt();
        if (answer1 == yes)
        System.out.println("Cool ~");
        else
        System.out.println("Ehh...");
    }
}

I want to put "yes" instead of the number 5. So if the user types "yes" it will print "correct".

P.S. I didn't find a clear answer to that in the search engine. It's not a duplicated thread as I'm trying to find a clear answer to that. I need a detailed explanation about it. I'm still a beginner, using those "high tech java words" won't help me.

Luminous
  • 31
  • 2
  • 6
  • That's much better - a small, complete example of code which illustrates the problem (in Java that's usually the smallest possible class with a `main` method and any imports which is exactly what you've got). – Flexo Apr 28 '13 at 13:27
  • So what do I do now? lol it gives an error and marks the "yes" as if it is not a valid word. – Luminous Apr 28 '13 at 13:34
  • It compiles and runs fine on my machine and at: http://ideone.com/3dFevM - perhaps you missed the quote marks out? – Flexo Apr 28 '13 at 13:37

2 Answers2

2

You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:

import java.util.Scanner;
public class Java {
    public static void main (String [] args) {

        Scanner scan = new Scanner(System.in);
        String answer1;
        System.out.println("Do you like Java?");
        answer1 = scan.next();
        if (answer1.equals("yes"))
        System.out.println("Cool ~");
        else
        System.out.println("Ehh...");
    }
}

I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.

Notice also that I've changed the test in the condition because it's now a String. See this answer for more on comparing Strings.

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Ohhh!!!! now I get it! thank you thank you! I not only copied pasted it, I also learned from it :) basically I needed to switch the int to String and the "==" to "equals" :D thank you! accepted this answer. and btw what if I want to use 3 words for example? like "Yes" or "yeah" or "yep" for example, what do I do then? lol – Luminous Apr 28 '13 at 13:38
  • @Luminous for completeness the reason you need to use `.equals`, for `String` but can't for `int` is because `int` is a primitive but `String` is an Object. – Flexo Apr 28 '13 at 13:41
  • How do I insert few words in the if statement? like "Yes" "Yep" and "Yeah" for example? I tried adding those words in any variable way, there must be something I'm missing. – Luminous Apr 28 '13 at 13:45
  • @Luminous `if (answer1.equals("yes") || answer1.equals("Yeah") || answer1.equals("..."))`. If you wanted to be clever you could use a `Collection` and search that, but for now using `||` as or is a good solution. – Flexo Apr 28 '13 at 13:48
  • Oh, the || thing, haha dam I almost got it, I tried with , and ((, well cool~ thanks bro! that helps a lot!! -- EDIT -- oh man ur THE BEST!!! That works smooth!! love it – Luminous Apr 28 '13 at 13:53
1

You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:

import java.util.Scanner; public class Java { public static void main (String [] args) {

    Scanner scan = new Scanner(System.in);
    String answer1;
    System.out.println("Do you like Java?");
    answer1 = scan.next();
    if (answer1.equals("yes"))
    System.out.println("Cool ~");
    else
    System.out.println("Ehh...");
} }

I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.

Notice also that I've changed the test in the condition because it's now a String. See this answer for more on comparing Strings.

Ok, what if you want the program to read both words and numbers: Here's my program (more in depth, when you see the full thing), but this is one of 5 parts (that look a like) where I'm having the program...

public static void Gdr1() {
        try {
            System.out.print("[Code: Gdr1]  Grade 1: %");
            Scanner gdr1 = new Scanner(System.in);
            Z = gdr1.next();
            Z = Double.toString(Grd1);
            Grd1 = Double.parseDouble(Z);
            if ((Grd1<100)&&(Grd1>=5)) {
                Gdr2();
            } else if ((Grd1>=100)&&(Grd1<125)) {
                System.out.println("    System> Great Job "+Stu+"!");
                Gdr2();
            } else if (Grd1<5) {
                System.out.println("I'm sorry, the lowest grade I am allowed to compute is 5...");
                Gdr1();
            } else if (Z.equalsIgnoreCase("restart")) {
                restart01();
            } else {
                System.out.println("("+Z+") cannot be resolved in my system...");
                Gdr1();
            }
        } catch (Exception e) {}
    }

Now everything works in the program, besides for when the End-User's input = "restart", I know some of the code in the program seems complicated, but it does work (most of it), can anyone help me try to figure this out, its for my portfolio at my school due latest by 1/25/2017 @ 11:59 pm.

The things like Z (constant String), ""+Stu+"" (variable input), and [Code: Gdr1] are there for a purpose...

Shae Noble
  • 11
  • 8