Make Scanner read a String in java
I am converting command line java application to swing GUI. I have a Parser class that reads input from Scanner
Scanner in = new Scanner(System.in);
command = in.nextInt();
Is it possible to pass String instead of (System.in) ? Or, is there a better way to deal?
You can use nextLine()
of Scanner class to read string value entered in console.
Below is an example:
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter command:");
String command = in.nextLine();
JOptionPane.showMessageDialog(null, command);
}
}
Eneter string value in console:
Please enter command:
Java Command
If you want pass Strings in Scanner just use the next()
method
Scanenr in = new Scanner(System.in);
Stringt command = in.next();
Hope that helps
链接地址: http://www.djcxy.com/p/78466.html上一篇: 我如何测试使用Scanner和InputStream的方法?
下一篇: 让扫描器在java中读取一个字符串