使用Scanner解析文件中的输入。 JAVA

时间:2018-01-31 20:43:03

标签: java java.util.scanner

我是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)

1 个答案:

答案 0 :(得分:1)

如果我理解正确的问题,解决方案很简单: 在dirOut.hasNext()循环中的System.out.printf()下面添加以下行。

dirOut.nextLine();

您的输入模式有两行;这个额外的nextLine()正确地在文件中前进。