Check input type using Scanner - Java File Path IO

Java examples for File Path IO:Scanner

Description

Check input type using Scanner

Demo Code



import java.io.*;
import java.util.*;

public class InputTypeChecker {
  public static void main(String args[]) throws Exception {
    System.out.print("Enter Something: ");
    // Write code here
    Scanner scan = new Scanner(System.in);

    if (scan.hasNextInt()) {
      System.out.println("This input is of type Integer.");
    } else if (scan.hasNextFloat()) {
      System.out.println("This input is of type Float.");
    } else if (scan.hasNext()) {
      System.out.println("This input is of type string.");

    }/* www .j  a  va  2  s . c o  m*/

    else
      System.out.println("This is something else.");
  }
}

Related Tutorials