数字格式问题,新手项目

时间:2015-02-23 07:35:53

标签: java numberformatexception

我正在尝试编写一个加载电影数据库文件的程序,然后将该信息拆分为电影标题,年份和所有关联的actor。我将所有信息分开,但是我将问题转换为int,这是将字符串转换为int的问题。年份字符串的格式是(****),*是一年,例如1999.当我尝试使用解析时,我得到一个数字格式异常。我试过替换括号,但它只是给了我更多的错误!有什么想法吗?

public class MovieDatabase {

ArrayList<Movie> allMovie = new ArrayList<Movie>();

//Loading the text file and breaking it apart into sections
public void loadDataFromFile( String aFileName) throws FileNotFoundException{
    Scanner theScanner = new Scanner(aFileName);
    theScanner = new Scanner(new FileInputStream("cast-mpaa.txt"));

    while(theScanner.hasNextLine()){
        String line = theScanner.nextLine();
        String[] splitting = line.split("/" );
        String movieTitleAndYear = splitting[0];
        int movieYearIndex = movieTitleAndYear.indexOf("(");
        String movieYear = movieTitleAndYear.substring(movieYearIndex);
        System.out.println(movieYear);
        //this is where I have issues
        int theYear = Integer.parseInt(movieYear);
        String movieTitle = movieTitleAndYear.substring(0, movieYearIndex);
        ArrayList<Actor> allActors = new ArrayList<Actor>();
        for ( int i = 1; i < splitting.length; i++){
            String[] names = splitting[i].split(",");
            String firstName = names[0];
            Actor theActor = new Actor(firstName);
            ArrayList<Actor> allActor = new ArrayList<Actor>();
            allActor.add(theActor);
        }

        Movie theMovie = new Movie(movieTitle, theYear, allActors);
        allMovie.add(theMovie);

    }       
    theScanner.close();
}

输出:

(1967)

以下是我遇到的错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "(1967)"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at MovieDatabase.loadDataFromFile(MovieDatabase.java:27)

3 个答案:

答案 0 :(得分:1)

数字周围有括号。您可以更正文件,也可以使用以下方法删除括号:

String str = "(1967)";
System.out.println(str.substring(1, str.length()-1));
Output:
1967

在您的代码中,您使用了:

int movieYearIndex = movieTitleAndYear.indexOf("(");
String movieYear = movieTitleAndYear.substring(movieYearIndex);

因此,如果我的movieTitleAndYear字符串是&#34; hi(1947)&#34;,indexOf将给出索引&#34;(&#34; as 3和substring将开始从索引3读取字符串,其中包括&#34;(&#34;。您可以避免打开括号的一种方法是将子字符串行更改为:

String movieYear = movieTitleAndYear.substring(movieYearIndex + 1);//but still you have closing bracket.

如果您确定它总是四位数,那么您可以执行以下操作:

String movieYear = movieTitleAndYear.substring(movieYearIndex + 1, movieYearIndex  + 5);

答案 1 :(得分:0)

您的substring来电当前用括号括起一年,例如(1967)。您可以通过调用substring variant that accepts an endIndex来避免这种情况,只需获取年份的四位数字:

String movieYear = 
    movieTitleAndYear.substring(movieYearIndex + 1, // to get rid of "("
                                movieYearIndex + 5 // to get rid of ")" 
                               );

答案 2 :(得分:0)

您需要为“)”添加indexof。 代码段:

int movieYearOpenBracesIndex = movieTitleAndYear.indexOf("(");
int movieYearCloseBracesIndex = movieTitleAndYear.indexOf(")");
String movieYear = movieTitleAndYear.substring(movieYearOpenBracesIndex + 1, movieYearCloseBracesIndex);
System.out.println(movieYear);

这将给出确切的年份。例如1967年