代码:
public static void createMovie() {
String fileName = "C:\\Users\\kat7r\\eclipse-workspace\\Final OOPs project\\src\\movielist.txt";
File movieFile = new File(fileName);
try {
Scanner inputStream = new Scanner(movieFile);
inputStream.next();
while (inputStream.hasNext()) {
String movieArgs = inputStream.next();
String[] splitData = movieArgs.split(",");
System.out.println(movieArgs);
//int priority = Integer.parseInt(splitData[0]);
//System.out.println("Priority: "+priority);
}
} catch(FileNotFoundException e) {
System.out.println("File not found.");
预期输出:
优先级,名称,流派,长度,评分
1,Inception,Science Fiction,2.47,8
2,Shawshank Redemption,Drama,2.37,9
3,可可,幻想,1.82,8
4,戒指之王,幻想,3.8,9 5,Thor:Ragnarok,Fantasy,2.17,8实际输出
1,初始,科学
小说,2.47,8
2,肖申克
赎回,剧情,2.37,9
3,可可,幻想,1.82,8
4,主
的
的
环,幻想,3.8,9
5,托尔:
仙境,幻想,2.17,8
注释:
本质上,空格正在使IDE启动新行。我尝试使用.nextLine
,但这没有帮助。尝试上线。
答案 0 :(得分:2)
您使用过:
inputStream.nextLine();
while(inputStream.hasNextLine())
{
String movieArgs = inputStream.nextLine();
String[] splitData = movieArgs.split(",");
System.out.println(movieArgs);
}
答案 1 :(得分:1)
由于您想要阅读输入的整行,请使用inputStream.nextLine()
代替inputStream.next()
。请参阅此topic。
它对我有用:
public static void createMovie() {
String fileName = "C:\\Movies.txt";
File movieFile = new File(fileName);
try {
Scanner inputStream = new Scanner(movieFile);
while (inputStream.hasNextLine()) {
String movieArgs = inputStream.nextLine();
System.out.println(movieArgs);
}
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}