簡體   English   中英

在java中從用戶獲取整數數組輸入

[英]Get integer array input from user in java

我需要從用戶那里獲得一系列整數。 系統不會提示用戶輸入數字。 輸入將具有以下形式:

6

34 12 7 4 22 15

3

3 6 2

第一行表示第二行中的整數數。 我首先嘗試將第二行作為String讀取,然后使用StringTokenizer將其分解為代碼中的整數。 正常情況發生在我的程序中很多,這很耗時,我需要直接閱讀它們。 我記得在C ++中它過去相當簡單。 以下代碼段用於執行此操作。

for(i=0;i<6;i++)
cin>>a[i];

為此,我使用了java.util.Scanner,我的代碼如下所示:

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
Scanner src = new Scanner(System.in);

for(x=0;x<2;x++){
    arraySize = Integer.parseInt(br.readLine());

    for(i=0;i<arraySize;i++)
        array[i] = src.nextInt();
}

在這種情況下,我收到以下錯誤:

Exception in thread "main" java.lang.NumberFormatException: For input string: "34 12 7 4 22 15"

我願意接受建議,而不是僅僅依靠Scanner。 如果有任何其他方法來實現這一點,我就是游戲。

為了將來參考,這里有一種方法可以使用Scanner類:

import java.util.Arrays;
import java.util.Scanner;


public class NumberReading {
    public static int [] readNumsFromCommandLine() {

        Scanner s = new Scanner(System.in);

        int count = s.nextInt();
        s.nextLine(); // throw away the newline.

        int [] numbers = new int[count];
        Scanner numScanner = new Scanner(s.nextLine());
        for (int i = 0; i < count; i++) {
            if (numScanner.hasNextInt()) {
                numbers[i] = numScanner.nextInt();
            } else {
                System.out.println("You didn't provide enough numbers");
                break;
            }
        }

        return numbers;
    }

    public static void main(String[] args) {
        int[] numbers = readNumsFromCommandLine();
        System.out.println(Arrays.toString(numbers));
    }
}

你說你可以在C ++中使用以下代碼:

for(i=0;i<6;i++) 
cin>>a[i];

如果這是您在C ++中使用的,為什么不使用它呢?

for(i=0;i<6;i++)
a[i] = input.nextInt();

假設掃描器聲明是:

Scanner input = new Scanner(System.in);

從我所看到的,人們正試圖為你編寫整個程序。 但正如你所說,你可以使用C ++代碼解決這個問題,我只是為你將代碼轉換為Java。

如果你想要完整的代碼,這里是:

import java.util.Scanner;

public class arrayFun {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // Create a new array. The user enters the size
        int[] array = new int[input.nextInt()];

        // Get the value of each element in the array
        for(int i = 0; i < array.length; i++)
            array[i] = input.nextInt();

        /* Note that the user can type the following:
         * 6
         * 34 12 7 4 22 15
         * This will set the array to size 6
         * array[0] = 34;
         * array[1] = 12;
         * etc.
         */

    }

}

我意識到這種反應已經晚了很多年,但當我遇到這個問題時,我正在尋找一個不同的問題。 這里的答案讓我很困惑,直到我意識到這不是我想要的。 看來這仍然出現在谷歌上,我決定發布這個。

    Scanner in = new Scanner(inputFile);
    int[] ints = Arrays.stream(in.nextLine().split("\\s+"))
      .mapToInt(Integer::parseInt).toArray();

如果你的輸入是“6 34 12 7 4 22 15 3 3 6 2”,用空格分隔,那么它將由br.readLine()一次性讀取。 正如您所見,您無法將“6 34 12 7 4 22 15 3 3 6 2”傳遞給parseInt

對於這種情況,我建議你在br.readLine()返回的String上調用String.split() ,然后你將得到一個字符串數組,它們可以依次傳遞給Integer.parseInt()

您可以嘗試使用StringTokenizer ,但如果您知道提前使用了哪個分隔符,只需使用String.split()

new Scanner(System.in).useDelimiter("[ ]") 

應該幫助你避免Vanathi在他的回答中提到的問題。 至於知道數組的大小,例如:

br.readLine().split("[ ]").length;

應該這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM