嗨所以我有这个项目需要我用java编写代码可以说我有这个txt文件:
GoodTitle Description
Gold The shiny stuff
Wheat What wheaties are made of
Wood To make more ships
Spices To disguise the taste of rotten food
Tobacco Smoko time
Coal To make them steam ships go
Coffee Wakes you up
Tea Calms you down
我要做的就是将文本的左侧(goodtitle、gold、wheat、wood 等)放入一个数组列表中,将文本的右侧(描述、闪亮的东西)放入另一个数组列表中。这是我当前的代码:
public void openFile(){
try{
x = new Scanner(new File("D://Shipping.txt"));
}
catch (Exception e){
System.out.println("File could not be found");
}
}
public void readFile(){
while (x.hasNextLine()){
String a = x.next();
x.nextLine();
ArrayList<String> list = new ArrayList<String>();
while (x.hasNext()){
list.add(x.next());
}
System.out.printf("%s \n", list);
}
}
public void closeFile(){
x.close();
可能它需要对 readFile 进行一些修改,因为我仍然对如何做到这一点感到困惑。提前致谢...
NOTE=I am not allowed to change the content of the txt file.
in my current code i still put the whole thing into 1 arraylist because i am unable to split them.
我需要 toString 方法吗?因为我不知道该怎么做。提前致谢...