java - 使用 Scanner 解析文件的输入。 java

标签 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)

最佳答案

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

dirOut.nextLine();

您的输入模式有两行;这个额外的 nextLine() 可以正确地遍历文件。

关于java - 使用 Scanner 解析文件的输入。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48551099/

相关文章:

java - 在 Android 中使用 IExtendedNetworkService 获取 USSD 响应

java - 使用 Java Netscape Directory LDAP API 和二进制过滤器值

java - JTable显示char[]

java - 扫描仪和文本中的字母 "Ł"出现问题 - JAVA

java - java程序中通过互联网进行更新检查

java - 扫描仪仅拾取某些输入

java - 从相机捕获图像并设置到android中的 ListView 中

java - JPanel 中的 GLCanvas 不起作用

java - 如何从java中的文本文件中获取行数?

java - 对所有 for 循环参数使用扫描仪输入