-3

I'm not sure how to explain this problem but please someone help me. I want to have a program to where if a word is typed into the keyboard, I have an if statement that makes it so that when that word is entered it prints out a sentence on the screen. Like, if I typed in "dog" and hit enter the screen would display information on dogs or something.

Tiny
  • 24,933
  • 92
  • 299
  • 571
JP96
  • 1
  • 1
  • 4
    "_I'm not sure how to explain this problem_" The problem seems to be that you haven't done anything yet. Have you made any attempt to accomplish this? If you have post that code here and explain what isn't working about it. – takendarkk Nov 26 '14 at 17:00

3 Answers3

0

You could use the Scanner to gather input from the keyboard, and then have an if statement to decide if the inputted string matches any string you want to present information about.

public void awesome(){
        Scanner scannerMcgee = new Scanner(System.in);
        String input = scannerMcgee.nextLine();
        if(input.equalsIgnoreCase("dog"))
            System.out.println("Information magically displays about " + input);
    }
austin wernli
  • 1,803
  • 9
  • 15
0

I'm a bit confused about what you want. Here is the first part of the solution to your problem.

import java.util.Scanner;

public class DogClass {
         public static void main(String[] args){
                Scanner input = new Scanner(System.in);
                System.out.println("please enter a word");
                String dogType = input.nextLine();
                System.out.println("the thing you chose was " + dogType);   
         }
}

The problem will ask you to enter a word and then the word will be printed. Please explain your problem further.

Robert
  • 1
  • 2
0

To achieve this functionality, if you don't have much entries you can use Hashmap to store your keys as word and its detailed information as values. When user enters word for search, you can search it out in map.

Please see the following example for single word search:

import java.util.HashMap;
import java.util.Scanner;

public class SearchText {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    HashMap wordMap = new HashMap();
    wordMap.put("Dog", "Dog is a nice pet");
    wordMap.put("Cat", "Cat is a beautiful pet");

    System.out.println("please enter a word: ");
    String inputWord = input.nextLine();

    if (wordMap.containsKey(inputWord)){
        System.out.println("Key: " + inputWord + ": " + wordMap.get(inputWord));
    } else {
        System.out.println("Please enter correct word");
    }
  }

}

If you have huge data, you can store it in any database table and whenever user enters the keyword, you can search the database using query for keyword and return result as needed.

Abhijeet Dhumal
  • 1,704
  • 11
  • 23