简体   繁体   English

单行Java扫描器从文件中读取矩阵

[英]One-line Java scanner to read a matrix from a file

I have been using this; 我一直在用这个; A kind of one-liner: 一种单线:

public static String[] ReadFileToStringArray(String ReadThisFile) throws FileNotFoundException{
    return (new Scanner( new File(ReadThisFile) ).useDelimiter("\\A").next()).split("[\\r\\n]+");
}

To read a file with this type of contents(ie with string tokens): 要读取具有这种类型的内容(即带有字符串标记)的文件:

abcd A B C D
abbd abbd
dbcd DBCD

But, now my file contents are something like this: 但是,现在我的文件内容是这样的:

1 2 3 4 1 2 3 4
1 2 2 4 1 2 2 4
1 5 3 7 1 5 3 7
1 7 3 8 1 7 3 8

I want these values to be read as integer. 我希望将这些值读取为整数。

I have seen these 1 , 2 and 3 questions but they do not answer to my question. 我已经看到了这123的问题,但他们不回答我的问题。

I have tried the following but failed: 我尝试了以下操作,但失败了:

public static int[][] ReadFileToMatrix(String ReadThisFile) throws FileNotFoundException{
    return (new Scanner( new File(ReadThisFile) ).useDelimiter("\\A").nextInt()).split("[\\r\\n]+");
}

Error message: Cannot invoke split(String) on the primitive type int . 错误消息: 无法在原始类型int上调用split(String) I understand the message and know it's horribly wrong :) 我了解此消息,并且知道这是非常错误的:)

Can anybody suggest the right way to achieve this. 任何人都可以提出实现此目标的正确方法。

PS With all due respect, "NO" to solutions with loops. PS在所有应有的尊重下,对带有循环的解决方案“不”。

Seems like an over complication of using classes when a basic BufferedReader with Integer.parseInt(line.split(" ")[n]); 带有Integer.parseInt(line.split(" ")[n]);的基本BufferedReader时,使用类似乎过于复杂Integer.parseInt(line.split(" ")[n]); will do. 会做。

If you use Java 7 or higher you can use something like this. 如果您使用Java 7或更高版本,则可以使用类似的内容。 I cannot think of a way to do it in one line without a loop. 我想不出一种无循环的方法。 Just put this into a methode and call it. 只需将其放入方法并调用即可。

//Read full file content in lines
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);

int NR_OF_COLUMNS = 4;
int NR_OF_ROWS = lines.size();

int[][] result = new int[NR_OF_ROWS][NR_OF_COLUMNS];

for(int rowIndex = 0; rowIndex < NR_OF_ROWS; rowIndex++)
{
    String[] tokens = lines.get(rowIndex).split("\\s+");  //split every line
    for(int columnIndex = 0; columnIndex < NR_OF_COLUMNS; columnIndex++)
        result[rowIndex][columnIndex] = Integer.parseInt(tokens[columnIndex]);   //convert every token to an integer
}
return result;

With Java 8, you can use Lambdas: 使用Java 8,您可以使用Lambda:

public static int[][] readFileToMatrix(String readThisFile) throws FileNotFoundException{
    return Arrays.stream((new Scanner( new File(readThisFile) ).useDelimiter("\\A").nextInt()).split("[\\r\\n]+")).mapToInt(Integer::parseInt).toArray();
}

Otherwise you cannot do it without a loop. 否则,您将无法没有循环。 You have a String[] array, and you want to call Integer.parseInt() for each element, one by one. 您有一个String[]数组,并且想要为每个元素一个一个地调用Integer.parseInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM