![](/img/trans.png)
[英]Store occurences of words in a file and their count,using Scanner.( Java )
[英]Parsing input from a file using Scanner. JAVA
我是java的新手,并且正在创建一个文件目录,该目录存储电影和有关电影的信息。 电影和信息存储在这样的文件中:
Movie Title
Rating Year Reviews Month Day Year
第一行的标题,第二行的其他信息。 因此,对于多部电影,它看起来像这样:
Movie Title
Rating Year Reviews Month Day Year
Movie Title
Rating Year Reviews Month Day Year
Movie Title
Rating Year Reviews Month Day Year
...等等。 我有一种将影片打印到控制台的方法,但是我遇到了正确格式化影片的问题。 电影信息应采用以下格式:
Star Wars PG 1977 5 stars 1/5/2018
每列应正确对齐。 标题,等级和年份应保持对齐; 星号和日期正确对齐。
第一次它完全按照我想要的方式打印电影信息,但是当我添加多部电影时,事情就变得混乱了。 这是我的代码,我相信问题出在listMovies方法中,我尝试格式化输出,但是我包括了所有代码,以防其他地方发生混乱。
public class Directory
{
private static final String dir = "data/cs2410-directory.data";
String mTitle,mRat,mRel,mRev,mM,mD,mY;
public Directory()
{
mTitle = "Movie Title";
mRat = "Rating";
mRel = "Year";
mRev = "Reviews";
mM = "Month";
mD = "Day";
mY ="Year";
insertMovie();
listMovies();
}
private void insertMovie()
{
PrintWriter dirIn = null;
try
{
dirIn = new PrintWriter(new FileOutputStream(dir,true));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
dirIn.println(mTitle);
dirIn.println(mRat+" "+mRel+" "+mRev+" "+mM+" "+mD+" "+mY);
dirIn.close();
System.out.print("The following movie has been added to the directory:\n");
System.out.print(mTitle+" ("+mRel+") "+mRat+"\n");
System.out.print("Stars: "+mRev+"\n");
System.out.print("Last Watched: "+mM+"/"+mD+"/"+mY+"\n");
}
private void listMovies()
{
Scanner dirOut = null;
try
{
dirOut = new Scanner(new FileReader(dir));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
while (dirOut.hasNext()) //PROBLEM
{
System.out.printf("%-15s%-10s%-10s%5s%10s/%s/%s\n", dirOut.nextLine(),dirOut.next()
,dirOut.next(),dirOut.next(),dirOut.next(),dirOut.next(),dirOut.next());
}
dirOut.close();
}
public static void main(String[] args)
{
new Directory();
}
这是一部电影(一次运行程序)后的输出,应该是这样的样子:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
这是运行两次之后的结果:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
Movie Exception in thread "main" java.util.NoSuchElementException
Title Rating Year/Reviews/Month
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1381)
at cs2410.assn3.directory.Directory.listMovies(Directory.java:71)
at cs2410.assn3.directory.Directory.<init>(Directory.java:32)
at cs2410.assn3.directory.Directory.main(Directory.java:79)
这是运行5次后的结果:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1381)
at cs2410.assn3.directory.Directory.listMovies(Directory.java:71)
at cs2410.assn3.directory.Directory.<init>(Directory.java:32)
at cs2410.assn3.directory.Directory.main(Directory.java:79)
如果我正确理解了该问题,则解决方案非常简单:在dirOut.hasNext()循环中的System.out.printf()下面添加以下行。
dirOut.nextLine();
您的输入模式有两行; 这个额外的nextLine()可以正确地浏览文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.