我有这种特定格式的文件:
0 2 4 0 1 安 0 5 乙 1 1 安 1 3 乙 2 6 安 2 4 乙 3 6 安 3 4 乙 4 6 安 4 4 乙 5 1 安 5 5 乙 6 6 安 6 2 乙
- 第 1 行 = 开始状态
- 第 2 行 = 接受状态
- 第 3 行 - n = 转换表
- 第一行 = 状态
- 第二行 = 状态输出
- A,B = 符号
我如何FileReader在 Java 中将这些文件读入 5 个不同ArrayList的 s(开始状态、最终状态、状态输入、状态输出和符号)?
我有这种特定格式的文件:
0 2 4 0 1 安 0 5 乙 1 1 安 1 3 乙 2 6 安 2 4 乙 3 6 安 3 4 乙 4 6 安 4 4 乙 5 1 安 5 5 乙 6 6 安 6 2 乙
我如何FileReader在 Java 中将这些文件读入 5 个不同ArrayList的 s(开始状态、最终状态、状态输入、状态输出和符号)?
最好在这里使用扫描仪:
static class State {
int in;
int out;
String symbol;
State(int in, int out, String symbol) {
this.in = in;
this.out = out;
this.symbol = symbol;
}
@Override
public String toString() {
return in + " " + out + " " + symbol;
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(new File("input.txt"));
int startState = Integer.parseInt(s.nextLine());
List<Integer> acceptStates = new LinkedList<Integer>();
List<State> states = new LinkedList<State>();
Scanner st = new Scanner(s.nextLine());
while (st.hasNextInt())
acceptStates.add(st.nextInt());
while (s.hasNextInt())
states.add(new State(s.nextInt(), s.nextInt(), s.next()));
System.out.println(startState);
System.out.println(acceptStates);
System.out.println(states);
// logic...
}
您可以使用Scanner类来读取文件(强烈推荐 nextLine())。由于您知道所需项目的位置,因此您可以使用split方法在您喜欢的任何 ArrayList 中解析输入字符串。
看看StringTokenizer,使用ArrayLists 来建立你的列表。