我想将我的文本文件内容读入多维数组。我的文本文件由特定格式的字符串组成。
我只想将其存储到多维数组中。我尝试使用java代码存储到字符串或字符串数组中,但我不知道如何将此格式存储到多维数组中。任何人都可以帮助我吗?
我的文字文件 read.txt 由
组成 { { "Kim","is" "playing" }, { "NOUN", "VERB", "DET" } },
{ { "Shine","is" "eating"}, { "NOUN","DET" "VERB" } },
{ { "Kevin","lives","in","Holland"}, { "NOUN", "VERB ","DET","Holland"} }
我尝试的是:
public class MyClass
{
static final String[][][] MULTI_ARRAY = new String[][][];
public static void main(String args[]
{
for (String[][] myarray : MULTI_ARRAY)
String[] sentences = myarray[0];
String[] partsofspeech = myarray[1];
}
}}
BufferedReader br=new BufferedReader(new FileReader("read.txt"));
while(br.ready)
{
MULTI_ARRAY[][][]=br.readLine();
}
}
即我希望我的3d数组采用以下格式
static final String[][][] MULTI_ARRAY = new String[][][] {
{ { "Kim","is" "playing" }, { "NOUN", "VERB", "DET" } },
{ { "Shine","is" "eating"}, { "NOUN","DET" "VERB" } },
{ { "Kevin","lives","in","Holland"}, { "NOUN", "VERB ","DET","Holland"} }
};
答案 0 :(得分:2)
假设格式为每行1个2D块。我还使用了ArrayList
而不是数组,因为您事先并不知道所需的大小。这是我的代码:
Scanner scan = new Scanner(new File("file.txt"));
List<List<List<String>>> d3 = new ArrayList<>();
while (scan.hasNextLine()) {
String line = scan.nextLine();
String[] splitted = line.split("(?<!\\\\)\\\"");
List<List<String>> d2 = new ArrayList<>();
d3.add(d2);
List<String> d1 = new ArrayList<>();
d2.add(d1);
// ignore first and last
for (int i = 1; i < splitted.length - 1; i++) {
if ((i & 1) != 0) { // odd, add to list
// unescape double quote and backslash
d1.add(splitted[i].replace("\\\"", "\"").replace("\\\\", "\\"));
} else { // even test if new array starts
if (splitted[i].matches(".*\\{.*")) {
d1 = new ArrayList<>();
d2.add(d1);
}
}
}
}
scan.close();
System.out.println(d3);
这个想法是基于"
被追究的分割线,但是,如果你逃避它们(\"
),你仍然可以在你的字符串中有引号并感谢negative lookbefore它会正确分裂。但如果字符串末尾有反斜杠,它将失败。
修改强>
要从List<List<List<String>>>
转换为String[][][]
,您可以使用以下代码:
String[][][] result = new String[d3.size()][][];
for (int i = 0; i < result.length; i++) {
result[i] = new String[d3.get(i).size()][];
for (int j = 0; j < result[i].length; j++) {
result[i][j] = new String[d3.get(i).get(j).size()];
for (int k = 0; k < result[i][j].length; k++) {
result[i][j][k] = d3.get(i).get(j).get(k);
}
}
}
System.out.println(Arrays.deepToString(result)); //see if result is ok
答案 1 :(得分:1)
另见答案:Rendering newlines in user-submitted content (Python web app)
我认为它是一台状态机
[Start]
|
v
[Base-State]--{--->[2D-State]---{---->[1D-State]
| ^ | ^ |
| |-----}---- | read everything until next } into buffer,
EOF | split by ',' into list,
| | append to multidimensional array
V | |
[End] |----------------
以下是如何使用伪代码构建简单的状态机:
state = "Base-State"
buff = ""
parser: while(true) {
currChar = readchar()
switch case(state) {
case "Base-State":
if (currChar == '{') {
state = "2D-State"
}
else if (currChar == null) {
break parser
}
break
case "2D-State":
if (currChar == '{') {
state = "1D-State"
}
else if (currChar == '}') {
state = "Base-State"
}
else if (currChar == null) {
//throw error
}
break
case "1D-State":
if (currChar == '{') {
//throw error
}
else if (currChar == null) {
//throw error
}
else if (currChar == '}') {
//split buffer into list, put into array at fitting place
state = "2D-State"
}
else {
buffer += currChar
}
break
}
}
所以 - 每个状态对应于switch中的一个case,每个转换都包含一个带有状态赋值的if语句,转换中的每个字符代表if语句中的条件。
答案 2 :(得分:0)
您应该尝试解析Textfile。 我发现这个教程用于解析,但你也可以解析你的文本文件。
parser tutorial
你所尝试的并不像是有效的。您必须了解您的代码已被遵守。但是文件文件部分在编译后加载,并且bufferedreader读取的文本将不会被编译,并且编译的程序无法读取它。