1

I know I can check for other datatypes, such as Integer, Double etc using

scanner.hasNextInt();
scanner.hasNextDouble();

But I don't think there is one for strings.

How can I check for String input?

Edit:

I'm working on a hangman game to practice more Java, and I need to make sure the player enters a word that can be used for the other player to guess.

I'm new to programming and did not know that a string meant anything.

Adam Jarvis
  • 183
  • 1
  • 13
  • 2
    `.hasNext()` ....... – Avinash Raj Oct 07 '15 at 18:39
  • @AvinashRaj Doesn't that take any input though? I need to make sure the input is not anything other than a String. – Adam Jarvis Oct 07 '15 at 18:41
  • 2
    @AdamJarvis: What series of characters *wouldn't* be a string? A string can literally be *anything*. – David Oct 07 '15 at 18:43
  • 1
    @AdamJarvis how do you define "not anything other than a string". You mean if its "123" that is not a string? – ControlAltDel Oct 07 '15 at 18:44
  • @David You are right. So is there a way to specifically check for a human language _word_? – Adam Jarvis Oct 07 '15 at 18:45
  • 4
    @AdamJarvis: That's a *much* bigger question than you probably realize. There are *many* languages, with *many* words. And even words which don't actually exist in any given dictionary are still perfectly cromulent words. Do you require correct spelling? What about names? Anything can be a name. How do you define "a human language word"? Linguistically, anything which expresses an idea from one entity to another is valid language. – David Oct 07 '15 at 18:48
  • They have dictionaries for that. –  Oct 07 '15 at 18:52
  • This belongs to the domain of Natural Language Processing. – Some guy Oct 07 '15 at 18:55
  • So this started off as me trying to make a hangman game to practice more Java. I'm trying to make sure the player enters a _word_ into the original input for the other player to guess – Adam Jarvis Oct 07 '15 at 19:00
  • 1
    @AdamJarvis You really should update your question's title and content, then, to make it clear that you're asking about *words* and not `String`s. – azurefrog Oct 07 '15 at 19:03
  • As a personal opinion... Such a restriction seems artificially limiting. Users may *want* to use words which aren't dictionary words. Proper names, places, made-up but still commonly-known words, phrases, compound semantic structures, etc. It sounds like you're trying to use technology to solve a non-technical problem. If a user creates a game where the "word" is something like "f444-=ewf-=-.wrg" or even contains un-typable characters then people are likely to simply stop playing with that user. – David Oct 07 '15 at 19:09
  • Possible duplicate of [How to check whether given string is a word](http://stackoverflow.com/questions/11607270/how-to-check-whether-given-string-is-a-word) – azurefrog Oct 07 '15 at 19:22
  • You're going to have to get some dictionary file and that you can load into the game, and depending on what kind of other words you want to allow (names of places and such), you would have to add that to the file. Also, you can validate the string is a possible word by using a regular expression to make sure it only contains type-able letters and excludes numbers. – Austin Oct 07 '15 at 19:27

3 Answers3

0

If you want to check if this word is made of alphabet characters you can make a loop iterating through every character and checking its ascii code. For characters 'A' - 'Z' it will be char>64 && char<91.

If you want to check for real words you will definitly need to create a dictionary of words that can be used.

Vivico
  • 1
  • 1
0

You can try this code for basic validation

import java.util.Scanner;

public class InputValidation {

public static void main(String[] args) {
    String input;
    try {
        System.out.println("Enter the input");
        Scanner s = new  Scanner(System.in);

        input = s.next();
        if(input.matches(".*\\d.*")){
            System.out.println(" Contains digit only");
        } else{
            System.out.println(" Only String/words found");
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

} For better way let me know the exact functionality you want to implement

mohit sharma
  • 1,050
  • 10
  • 20
0

If you want the user to enter an actual word, you'll have to have a list of all permitted words, and preferably create a switch statement with all the allowed words. You can build a validator that looks something like this:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    System.out.print("Enter a word: ");
    Scanner answer = new Scanner(System.in);
    String word = answer.next();
    switch (word) {
      case "dog":
      case "cat":
      case "mouse":
      case "elephant":
      case "lion":
      // List all your words above...
      System.out.println("Word accepted.");
      break;
      default: // If user inputs a word that is not in list.
      System.out.println("You must enter a qualified word!");   
    }
  }  
}

EDIT

If you have a lot of words, a better way might be to create a .txt file, and store all words inside the file, one word for each line. Then search use the java program to search the txt file to see if the word is in te txt file.

ublec
  • 172
  • 2
  • 15