尝试使用正则表达式读取文本文件以检查每一行

时间:2014-10-09 03:26:11

标签: java regex arraylist java.util.scanner

我正在尝试编写一个允许用户输入电影名称的程序,然后该程序将生成与之关联的日期。我有一个文本文件,其中包含日期和与之相关的电影。我正在通过Scanner读取文件,我创建了一个电影类,分别存储电影和日期的ArrayList和String。我在阅读文件时遇到了麻烦。任何人都可以帮助我。谢谢!

以下是文本文件的一部分:

10/1/2014   
    Der Anstandige
    "Men, Women and Children"
    Nas: Time is Illmatic

10/2/2014   
    Bang Bang
    Haider

10/3/2014   
    Annabelle
    Bitter Honey
    Breakup Buddies
    La chambre bleue
    Drive Hard
    Gone Girl
    The Good Lie
    A Good Marriage
    The Hero of Color City
    Inner Demons
    Left Behind
    Libertador
    The Supreme Price

这是我的电影课

import java.util.ArrayList;

public class movie
{
    private ArrayList<String> movies;
    private String date;

    public movie(ArrayList<String> movies, String date)
    {
        this.movies = movies;
        this.date = date;
    }

    public String getDate()
    {
        return date;
    }

    public void setDate(String date)
    {
        this.date = date;
    }

    public ArrayList<String> getMovies()
    {
        return movies;
    }
}

这是readFile类

package Read;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class readFile
{
    public static List<movie> movies;
    public static String realPath;
    public static ArrayList<String> mov;
    public static String j;
    public static String i;

    public static void main(String[]args)
    {
        //movies = new ArrayList<movie>();
        realPath = "movie_release_dates.txt";
        File f = new File(realPath);
        try
        {
            String regex1 = "[^(0-9).+]";
            String regex2 = "[^0-9$]";

            Scanner sc = new Scanner(f);

            while (sc.hasNextLine())
            {
                System.out.println("Hello");
                //movies
                if(!sc.nextLine().matches(regex2))
                {
                    i = sc.nextLine();
                    System.out.println("Hello2");
                    System.out.println(i);
                }
                //date
                while(sc.nextLine().matches(regex1))
                {
                    System.out.println("Hello3");
                    if(!sc.nextLine().matches(regex1))
                    {
                        j = sc.nextLine();
                        mov.add(sc.nextLine());
                        System.out.println("Hello4");
                    }
                }
                movie movie = new movie(mov,i);
                movies.add(movie);
            }

        //   sc.close();
        }
        catch(Exception e)
        {
            System.out.println("CANT");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

package com.stackoverflow.q26269799;

import java.util.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class ReadFile {
    public static List<Movie> movies = new ArrayList<Movie>();
    public static String realPath;
    public static ArrayList<String> mov;
    public static String j;
    public static String i;

    public static void main(String[] args) {
        //movies = new ArrayList<movie>();
        realPath = "movie_release_dates.txt";
        File f = new File(realPath);
        if ( !f.exists()) {
            System.err.println("file path not specified");
        }
        try {
            String regex1 = "[^(0-9).+]";
            String regex2 = "[^0-9$]";

            Scanner sc = new Scanner(f);

                while (sc.hasNextLine()) {
                    System.out.println("Hello");
                    // movies
                    String nextLine = sc.nextLine();
                    if (nextLine != null) {
                        if ( !nextLine.matches(regex2)) {
                            i = nextLine;
                            System.out.println("Hello2");
                            System.out.println(i);

                        }
                        // date
                        while (nextLine != null && nextLine.matches(regex1)) {
                            System.out.println("Hello3");
                            if ( !nextLine.matches(regex1)) {
                                j = nextLine;
                                mov.add(nextLine);
                                System.out.println("Hello4");

                            }                           
                            nextLine = sc.nextLine();
                        }
                    }
                    Movie movie = new Movie(mov, i);
                    movies.add(movie);
                }   

             //   sc.close();
             } catch(Exception e) {
                  throw new RuntimeException(e);
             }
    }
}
  1. 这是必需的://movies = new ArrayList<movie>();
  2. 每次调用nextLine时,它都会将扫描仪点移动到下一行。所以每次调用一次并检查它是否与正则表达式匹配。 String nextLine = sc.nextLine();
  3. 请检查您是否指定了文件路径。

答案 1 :(得分:1)

你不应该在每次检查中调用sc.nextLine()。每个NextLine()调用都会读取下一行。这意味着您正在检查一行并处理下一行

答案 2 :(得分:0)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class ReadFile
{
    Map<String, String> movies;

    public static void main(String[] args) throws IOException
    {
        ReadFile readFile = new ReadFile();

        readFile.movies = new TreeMap<>();

        try
        {
            readFile.importData();

            printf(readFile.queryData("Der Anstandige"));

            printf(readFile.queryData("Bitter"));

            printf(readFile.queryData("blah"));

            printf(readFile.queryData("the"));
        }
        catch(IOException e)
        {
            throw(e);
        }
    }

    void importData() throws IOException, FileNotFoundException
    {
        LineNumberReader reader = null;

        File file = new File("c:/movie_release_dates.txt");

        try
        {
            reader = new LineNumberReader(new FileReader(file), 1024*64);    //

            String line;

            String date = null, movie = null;

            while((line = reader.readLine()) != null)
            {
                line = line.trim();

                if(line.equals("")) continue;

                if(line.matches(PATTERN_DATE))
                {
                    date = line;
                    date = strf("%s/%s", 
                                date.substring(date.length() - 4),
                                date.substring(0, date.length() - 5));

                    continue;
                }
                else
                {
                    movie = line.trim();
                }

                movies.put(movie, date);
            }
        }
        catch(FileNotFoundException e)
        {

            throw(e);
        }
        finally
        {
            reader.close();
        }
    }

    String queryData(String title)
    {
        String regex = "(?i)" + title.replaceAll("\\s", "\\s+");

        String[] matches = new String[movies.size()];

        int i = 0; for(Entry<String , String> movie : movies.entrySet())
        {
            String key = movie.getKey();
            String val = movie.getValue();

            if(key.matches(regex))
            {
                matches[i++] = strf("{movie=%s, date=%s}", key, val);
            }
            else if(key.toUpperCase().trim()
                       .contains(title.toUpperCase().trim()))
            {
                matches[i++] = strf("{movie=%s, date=%s}", key, val);
            }
        }

        String string = "";

        if(matches[0] == null)
        {
            string = "Not found\n";
        }
        else
        {
            i = 0; while(matches[i] != null)
            {
                string += matches[i++] + "\n";
            }
        }

        return string;
    }

    final String strf(String arg0, Object ... arg1)
    {
        return String.format(arg0, arg1);
    }

    final static void printf(String format, Object ... args)
    {
        System.out.printf(format, args);
    }

    final static void println(String x)
    {
        System.out.println(x);
    }

    final String PATTERN_DATE = "\\d{1,2}\\/\\d{1,2}\\/\\d{4}";
}

控制台输出: {movie=Der Anstandige, date=2014/10/1} {movie=Bitter Honey, date=2014/10/3} Not found {movie=The Good Lie, date=2014/10/3} {movie=The Hero of Color City, date=2014/10/3} {movie=The Supreme Price, date=2014/10/3}