Java:如何轻松地从控制台输入中获取值
作为用户,我在java控制台中键入以下命令:Java:如何轻松地从控制台输入中获取值,java,string,console,split,readline,Java,String,Console,Split,Readline,作为用户,我在java控制台中键入以下命令: !login <Username> <udpPort> 我需要的是从该输入中轻松获取值: String username = "Bob"; int port = 2233; 我正在使用BufferedReader来获取输入 我已经试过了,但当然不行。但这正是我想要的: String [] input = in.readReadLine(); //ofcourse this is not working 然后我可以轻松地分
!login <Username> <udpPort>
我需要的是从该输入中轻松获取值:
String username = "Bob";
int port = 2233;
我正在使用BufferedReader来获取输入
我已经试过了,但当然不行。但这正是我想要的:
String [] input = in.readReadLine(); //ofcourse this is not working
然后我可以轻松地分配值:
String username = input[2]; //save "Bob"
int port = Integer.parseInt(input[3]); //save 2233
感谢您的建议,
Dave
BufferedReader
方法返回String
一旦获得字符串,您需要
Split()
(或)StringTokenizer
作为单独的字符串获取。bufferedreader
方法返回String
一旦获得字符串,您需要
Split()
(或)StringTokenizer
作为单独的字符串获取。我认为您可以使用java.util.Scanner
,如下所示:
Scanner inputScanner = new Scanner(System.in);
inputScanner.next(); //reads whole word
inputScanner.nextInt(); //reads whole numeral
inputScanner.nextLine(); //reads whole line
现在使用nextLine
,读取行并拆分
String line = inputScanner.nextLine();
String[] commands = line.split(" "); //splits space delimited words in the line
或者使用next()
,一次读取一条命令,例如
command[0] = inputScanner.next();
command[1] = inputScanner.next();
或者您可以使用next()
和nextInt()
String username = inputScanner.next(); //save "Bob"
int port = inputScanner.nextInt(); //save 2233
这里有更多的方法细节 我认为您可以使用
java.util.Scanner
如下:
Scanner inputScanner = new Scanner(System.in);
inputScanner.next(); //reads whole word
inputScanner.nextInt(); //reads whole numeral
inputScanner.nextLine(); //reads whole line
现在使用nextLine
,读取行并拆分
String line = inputScanner.nextLine();
String[] commands = line.split(" "); //splits space delimited words in the line
或者使用next()
,一次读取一条命令,例如
command[0] = inputScanner.next();
command[1] = inputScanner.next();
或者您可以使用next()
和nextInt()
String username = inputScanner.next(); //save "Bob"
int port = inputScanner.nextInt(); //save 2233
这里有更多的方法细节 你应该这样做
String [] input = in.readReadLine().split("\\s+"); // split the line at spaces
并使用索引1和2,因为数组索引从0开始,而不是从1开始
String username = input[1]; //get the second element of the array
int port = Integer.parseInt(input[2]); //get the third element and parse
你应该这样做
String [] input = in.readReadLine().split("\\s+"); // split the line at spaces
并使用索引1和2,因为数组索引从0开始,而不是从1开始
String username = input[1]; //get the second element of the array
int port = Integer.parseInt(input[2]); //get the third element and parse
Scanner类最适合从控制台获取输入
import java.util.*;
public class ConsoleInput {
public static void main(String[] args) {
Scanner scanInput = new Scanner(System.in);
String input = scanInput.nextLine();
System.out.println("The input is : "+ input);
}
}
这是一个简单的类,演示了在Java中使用Scanner类。它有几种方法可以帮助您读取不同类型的输入,如ex-
int
、char
、String
,等等。Scanner类最适合从控制台获取输入
import java.util.*;
public class ConsoleInput {
public static void main(String[] args) {
Scanner scanInput = new Scanner(System.in);
String input = scanInput.nextLine();
System.out.println("The input is : "+ input);
}
}
这是一个简单的类,演示了在Java中使用Scanner类。它有几种方法可以帮助您读取不同类型的输入,如ex-
int
、char
、String
,等等。检查是。我刚刚使用了:String[]sI=in.readLine().split(“”);对我刚刚使用了:String[]sI=in.readLine().split(“”);