Saturday, December 21, 2013

Reading input from Terminal in Java

public class ReadIO {

    public int readInt(Scanner sin) {
        return sin.nextInt();
    }
   
    public double readDouble(Scanner sin){
        return sin.nextDouble();
    }
   
    public String readString(Scanner sin){
        if(sin.nextLine() != "")
            return sin.nextLine();
        return sin.nextLine();
    }
   
    public char readChar(Scanner sin){
        return sin.next().charAt(0);
    }
   
    public float readFloat(Scanner sin){
        return sin.nextFloat();
    }
   
    public static void main(String args[]){
        ReadIO io = new ReadIO();
        Scanner sin = new Scanner(System.in);
        System.out.println("ReadInteger: " + io.readInt(sin));
        System.out.println("ReadDouble: " + io.readDouble(sin));
        System.out.println("ReadString: " + io.readString(sin));
        System.out.println("ReadChar: " + io.readChar(sin));
        System.out.println("ReadFloat: " + io.readFloat(sin));
    }
}

No comments:

Post a Comment