在Java中获取输入的不同方法是什么?
我使用了两种方法:
BufferedReader
和
Scanner
还有其他方法可以获得输入吗?
如果是这样,它们之间有什么区别?
答案 0 :(得分:3)
我尝试了几种方法,看看如何通过不同的对象输入的可能性,我用4种不同的方式探讨了它
public String input1()
{
System.out.println("enter the input");
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
return s1;
}
public String input2()throws IOError
{
Console c=System.console();
String s2=null;
s2=c.readLine("enter the value");
return s2;
}
public String input3()
{
System.out.println("enter the input");
String s3=javax.swing.JOptionPane.showInputDialog("enter the text");
return s3;
}
public String input4()throws Exception
{
System.out.println("enter the input");
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String s4=br.readLine();
return s4;
}
如果我再遇到,我一定会把它列在这里
答案 1 :(得分:0)