简体   繁体   中英

Using scanner in a switch statement but my program only keeps printing the default

I am a college freshman who's studying programming, so apparently, I'm new to this, but I'm already at the part of switch statements.

The goal of my program is to be like a dictionary. When user enters the term, the program should enter its meaning. But my program keeps printing the default . What do you think am I missing? Here's my code:

        Scanner scn = new Scanner(System.in);
            
        System.out.print("Enter the term:");
        String term = scn.nextLine();
            
        switch(term) {
            case "Programming":
                System.out.println("It is the process of creating a set of instructions to tell the computer how to do a task.");
            break;
            case "Java":
            System.out.println("A popular high-level and object-oriented programming language developed by Sun Microsystems in 1995 and now, owned by Oracle.");
            break;
            case "print":
                System.out.println("A method in Java that is used to display a text on the console.");
            break;
            case "scanner":
            System.out.println("It is used to get an input data from the user, and found in the java.util package.");
            break;
            case "packages":
                System.out.println("It organizes Java classes into namespaces, providing a unique namespace for each type it contains.");
            break;
            case "IDE":
                System.out.println("also known as Integrated Development Environment, a software app that enables us to write & debug programs more easily.");
            break;
            case "switch":
                System.out.println("Unlike conditionals, these statements only checks equality and only works strings, char, int and enums.");
            break;
            case "function":
                System.out.println("A block of organized, reusable code that is used to perform a single, related action.");
            break;
            case "Conditional Statements":
                System.out.println("It allows a program to take action based on the given condition.");
            break;
            case "IF-statements":
                System.out.println("It is one of the conditional statement that handles one conditional expression. It either does SOMETHING or NOTHING.");
            break;
            default: 
                System.out.println("Sorry, we don't have any information about that.");
                 }
                 
        scn.close();

Sorry, if it's a lengthy program, I want to make it ten items as possible. I also forgot to tell you that I tried once to remove the Scanner and only declare the variables, the program worked (just for you to know that my program also works and print strings).

Anyway, for those who will respond, thank you very much in advance!

Posted is working correctly, at least for input Java see IDEone .
Most probably the input is not correct - switch needs exact same text: case is important, spaces are important, ...

Suggestion: include the input in the error message:

default: 
    System.out.println("Sorry, we don't have any information about '" 
                       + term + "'");

so the message can help user (and developer) see what went wrong (even/specially true in production code).

As commented, for this user case, it is better to ignore case like in:

switch (term.toLowerCase()) {
    case "programming":   \\ the labels must all be lowercase too
        ...
    case "java":
        ...

Incase there is space at the end of your input, you can trim that term.trim()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM