代码:
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、盗梦空间,科幻小说,2.47,8
2,肖申克救赎,戏剧,2.37,9
3、可可、幻想、1.8、2、8
4,魔戒之王,幻想,3.8,9
雷神: Ragnarok,Fantasy,2.17,8
实际输出;
1,盗梦空间,科学
小说,2.47,8
2、肖申克
救赎,戏剧,2.37,9
3、可可、幻想、1.8、2、8
4,主
的
这个
戒指、幻想、3.8、9
5,雷神:
Ragnarok,幻想,2.17,8
评论:
实际上,这些空格导致IDE启动一个新的行。我试过使用.nextLine,但这没有帮助。也试过上网。
发布于 2018-04-30 22:11:49
你用过:
inputStream.nextLine();
while(inputStream.hasNextLine())
{
String movieArgs = inputStream.nextLine();
String[] splitData = movieArgs.split(",");
System.out.println(movieArgs);
}发布于 2018-04-30 22:05:47
因为您希望读取输入的整行,所以使用inputStream.nextLine()而不是inputStream.next()。请参阅此主题。
它适用于我:
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());
}
}https://stackoverflow.com/questions/50108793
复制相似问题