-4

大家好,我在尝试将第一行放入 10 个整数的数组时遇到问题。这是我到目前为止所拥有的

public class KnapEncrypt {

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("Testinput.txt");
    Scanner sc = new Scanner(file);

    while(sc.hasNext()){
    int line = sc.nextInt();
    System.out.println(line);
    }



 }
}

这是文件:

191 691 573 337 365 730 651 493 177 354

1000011100

1101000001

0000100010

0100000000

1028

2426

2766

1129

基本上我想要的是将第一行放入一个包含 10 个整数但不是其余数字的数组中

4

3 回答 3

3
BufferedReader reader = new BufferedReader(new FileInputStream(file));
String line = reader.readLine();
String[] lineSplitted = line.split(" ");
于 2013-07-13T14:51:48.253 回答
3
FileInputStream fis = new FileInputStream("your_file_here");

    Scanner scanner = new Scanner(fis);
    String firstLine = scanner.nextLine();

    firstLine.trim();
    String[] data = firstLine.split(" ");

    int[] intData = new int[data.length];

    for (int i = 0; i < intData.length; i++) {
        intData[i] = Integer.parseInt(data[i]);
    }
于 2013-07-13T14:59:32.200 回答
2

First read your line from the file:

String line = bufferedReaderForFile.readLine();

Then hand this to your scanner:

Scanner sc = new Scanner(line);
// your while loop here
于 2013-07-13T14:52:03.597 回答