Luminous
Luminous

Reputation: 31

How do I set the statement if to read letters instead of numbers?

"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.

Upvotes: 0

Views: 2872

Answers (2)

Shae Noble
Shae Noble

Reputation: 11

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...

Upvotes: 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.

Upvotes: 2

Related Questions