【发布时间】:2014-10-09 03:26:10
【问题描述】:
我正在尝试编写一个程序,允许用户输入电影的名称,然后该程序将生成与之关联的日期。我有一个包含日期和与之相关的电影的文本文件。我正在通过扫描仪读取文件,并创建了一个电影类,该类分别存储电影和日期的 ArrayList 和字符串。我在阅读文件时遇到问题。谁能帮助我。谢谢!
这是文本文件的一部分:
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");
}
}
}
【问题讨论】:
-
“你遇到麻烦了”是什么意思?是否抛出异常?如果是这样,什么例外,在哪里?它是在做一些意想不到的事情,还是没有做一些事情,如果是,那是什么?
-
@drewmoore 我的错,我没有得到每一行的字符串。所以问题出在我的代码上,特别是用于隔离日期并存储它的正则表达式,还隔离了该特定日期的电影列表。
标签: java regex arraylist java.util.scanner