【发布时间】:2015-04-22 20:49:07
【问题描述】:
我目前正在尝试获取文本文件中句子按顺序排列的输出:
0 : The cat in the hat
1 : The cat sat on the mat
2 : Pigs in a blanket
我将文本文件添加到 ArrayList 中,目前无法显示上述输出。我知道问题出在for loop
public static void main(String[] args) throws FileNotFoundException{
//Pass in file name as command line argument
File inFile = new File("test.txt");
//Open scanner to scan in the file and create Array List
try (Scanner scan = new Scanner(new FileInputStream(inFile)))
{
ArrayList<String> list = new ArrayList<>();
//Create while loop to read in sentences of the file
while(scan.hasNextLine())
{
String line = scan.nextLine();
list.add(line);
}
int i;
System.out.println("Input Sentences: ");
for(i = 0; i<inFile.length(); i++)
{
System.out.println(i + ":");
}
}
}
}
【问题讨论】:
标签: java string file for-loop arraylist