在java中,为了得到安全的数据输入,例如 int float double char ...需要先输入一个字符串,然后运用Double Float Integer 类提供的Xxx.praseXxx();方法得到所需的数据。一下是方法:

package exercise;

import java.io.*;

import java.util.*;

public class e3 {

public static void main(String[]args)

{

System.out.println("enter a int");

int a=getInt();

System.out.println("enter a float");

float b=getFloat();

System.out.println("enter a char");

char c=getChar();

System.out.println("enter a double");

double d=getDouble();

System.out.println("input");

System.out.println(a+" "+b+" "+c+" "+d);

}

public static String getString() throws IOException//得到一个字符串的函数

{

InputStreamReader str=new InputStreamReader(System.in);

BufferedReader s1=new BufferedReader(str);

String f=s1.readLine();

return f;

}

public static int getInt() throws IOException//得到int的函数

{

String s=getString();

return Integer.parseInt(s);

}

public static char getChar()throws IOException//得到char的函数

{

String s=getString();

return s.charAt(0);

}

public static double getDouble()throws IOException//得到double的函数

{

String s=getString();

return Double.parseDouble(s);

}

public static float getFloat()throws IOException//得到float的方法

{

String s=getString();

return Float.parseFloat(s);

}




}