Java从单行读取多个int

我正在研究一个程序,我希望允许用户在提示时输入多个整数。 我曾尝试使用扫描仪,但我发现它只存储用户输入的第一个整数。 例如:

输入多个整数:1 3 5

扫描仪只能获得第一个整数1.是否可以从一行获得所有3个不同的整数并且以后能够使用它们? 这些整数是我需要根据用户输入操作的链表中数据的位置。 我无法发布我的源代码,但我想知道这是否可行。

我总是在hackerearth上使用它

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String lines = br.readLine(); String[] strs = lines.trim().split("\\s+"); for (int i = 0; i < strs.length; i++) { a[i] = Integer.parseInt(strs[i]); } 

尝试这个

 public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { if (in.hasNextInt()) System.out.println(in.nextInt()); else in.next(); } } 

默认情况下,扫描程序使用分隔符模式“\ p {javaWhitespace} +”,它至少匹配一个空格作为分隔符。 你不必做任何特别的事。

如果要匹配空格(1或更多)或逗号,请将扫描程序调用替换为此

 Scanner in = new Scanner(System.in).useDelimiter("[,\\s+]"); 

Scanner有一个名为hasNext()的方法:

  Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { System.out.println(scanner.nextInt()); } 

你想把数字作为一个字符串,然后使用String.split(" ")来获得3个数字。

 String input = scanner.nextLine(); // get the entire line after the prompt String[] numbers = input.split(" "); // split by spaces 

数组的每个索引都将保存一个字符串表示的数字,可以通过Integer.parseInt()使其成为int

如果你知道你将得到多少整数,那么你可以使用nextInt()方法

例如

 Scanner sc = new Scanner(System.in); int[] integers = new int[3]; for(int i = 0; i < 3; i++) { integers[i] = sc.nextInt(); } 

以下是如何使用扫描程序处理用户想要输入的整数并将所有值放入数组中的方法。 但是,如果您不知道用户将输入多少个整数,则只应使用此方法。 如果您知道,您应该只使用Scanner.nextInt()您想要获得整数的次数。

 import java.util.Scanner; // imports class so we can use Scanner object public class Test { public static void main( String[] args ) { Scanner keyboard = new Scanner( System.in ); System.out.print("Enter numbers: "); // This inputs the numbers and stores as one whole string value // (eg if user entered 1 2 3, input = "1 2 3"). String input = keyboard.nextLine(); // This splits up the string every at every space and stores these // values in an array called numbersStr. (eg if the input variable is // "1 2 3", numbersStr would be {"1", "2", "3"} ) String[] numbersStr = input.split(" "); // This makes an int[] array the same length as our string array // called numbers. This is how we will store each number as an integer // instead of a string when we have the values. int[] numbers = new int[ numbersStr.length ]; // Starts a for loop which iterates through the whole array of the // numbers as strings. for ( int i = 0; i < numbersStr.length; i++ ) { // Turns every value in the numbersStr array into an integer // and puts it into the numbers array. numbers[i] = Integer.parseInt( numbersStr[i] ); // OPTIONAL: Prints out each value in the numbers array. System.out.print( numbers[i] + ", " ); } System.out.println(); } } 

您可能正在寻找String.split(String regex)。 使用“”作为你的正则表达式。 这将为您提供一组字符串,您可以将它们单独解析为整数。

这工作正常….

int a = nextInt(); int b = nextInt(); int c = nextInt();

或者你可以循环阅读它们

最好将整行作为字符串,然后使用StringTokenizer获取数字(使用空格作为分隔符),然后将它们解析为整数。 这将适用于一行中的n个整数。

  Scanner sc = new Scanner(System.in); List l = new LinkedList<>(); // use linkedlist to save order of insertion StringTokenizer st = new StringTokenizer(sc.nextLine(), " "); // whitespace is the delimiter to create tokens while(st.hasMoreTokens()) // iterate until no more tokens { l.add(Integer.parseInt(st.nextToken())); // parse each token to integer and add to linkedlist } 

在许多编码站点上使用它:

  • 案例1:每个行中的整数数量都给予了

假设给出3个测试用例,每行4个整数输入用空格分隔1 2 3 4 5 6 7 8 1 1 2 2

  int t=3,i; int a[]=new int[4]; Scanner scanner = new Scanner(System.in); while(t>0) { for(i=0; i<4; i++){ a[i]=scanner.nextInt(); System.out.println(a[i]); } //USE THIS ARRAY A[] OF 4 Separated Integers Values for solving your problem t--; } 
  • 情况2:每行中的整数数量没有给出

      Scanner scanner = new Scanner(System.in); String lines=scanner.nextLine(); String[] strs = lines.trim().split("\\s+"); 

    请注意,您需要先修剪(): trim().split("\\s+") - 否则,例如拆分abc将首先发出两个空字符串

      int n=strs.length; //Calculating length gives number of integers int a[]=new int[n]; for (int i=0; i 

使用BufferedReader

 StringTokenizer st = new StringTokenizer(buf.readLine()); while(st.hasMoreTokens()) { arr[i++] = Integer.parseInt(st.nextToken()); }