简体   繁体   English

为什么我收到输入不匹配错误 (Java)

[英]Why am i getting Input Mismatch Error (Java)

So far i have this.到目前为止,我有这个。

  import java.util.*;
  public class Moviesol{
  public static void main (String[] args){
    Scanner sc= new Scanner(System.in);
    Movie[] m = new Movie[3];
    for(int i=0;i<3;i++){
        String name =sc.nextLine();

        String company =sc.nextLine();

        String genre =sc.nextLine();

        Long budget =sc.nextLong();
        m[i] = new Movie(name, company, genre, budget);
    }

    String ip = sc.nextLine();

    MovieByGenre(m , ip);


}
public static void MovieByGenre(Movie[] m, String ip ){
    for(int i=0;i<3;i++){
        if(((m[i].getgenre()).equals(ip))&& (m[i].getbudget()>=80000000)){

                System.out.println("High Budget Movie");}
        else if(((m[i].getgenre()).equals(ip))&& (m[i].getbudget()<=80000000)){ 
                System.out.println("Low Budget Movie");
        }
    }
}
}

This is the other class这是另一个班级

class Movie {
private String name;
private String company;
private String genre;
private Long budget;
public Movie(String name, String company, String genre, Long budget){
    this.name=name;
    this.company=company;
    this.genre=genre;
    this.budget=budget;
}
public String getgenre(){
    return genre;
}
public Long getbudget(){
    return budget;
        }
}

I am using getter methods to grab info from other class.我正在使用 getter 方法从其他类中获取信息。 The program takes该程序需要

'name' of String type字符串类型的“名称”

'Company' of string type字符串类型的“公司”

'genre' of string type字符串类型的“流派”

'budget' of long type.长型的“预算”。

i need to match genre with the 'ip' String, if it matches, budget is compared , and prints accordingly我需要将流派与 'ip' 字符串匹配,如果匹配,则比较预算,并相应地打印

My input我的输入

asd
asd
asd asd
90000000000
asd
asd
asd asd
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextLong(Scanner.java:2373)
at java.base/java.util.Scanner.nextLong(Scanner.java:2328)
at Moviesol.main(Moviesol.java:13)

Please help!!!请帮忙!!!

The exception means that the input read from the file does not fir into the object you want to store it.异常意味着从文件读取的输入不会进入您要存储它的对象。 For example scanner.nextInt() thows that exception if the next element is "abcd".例如,如果下一个元素是“abcd”,scanner.nextInt() 就会抛出异常。

Your related code is:您的相关代码是:

        String name =sc.nextLine();
        String company =sc.nextLine();
        String genre =sc.nextLine();
        Long budget =sc.nextLong();

So for each movie you have to enter 3 lines of text and one long integer number.因此,对于每部电影,您必须输入 3 行文本和一个长整数。

asd asd is only one line, and surely not a number. asd asd只有一行,而且肯定不是数字。

import java.util.*;
public class Moviesol{
public static void main (String[] args){
Scanner sc= new Scanner(System.in);
Movie[] m = new Movie[3];
for(int i=0;i<3;i++){
    String name =sc.nextLine();

    String company =sc.nextLine();

    String genre =sc.nextLine();

    String strBudget =sc.nextLine();
    Long budget = null;
    try {
        budget = Long.parseLong(strBudget);
    } catch (NumberFormatException ex) {
        // what happens if they didn't enter a number
    }
    m[i] = new Movie(name, company, genre, budget);
}

String ip = sc.nextLine();

MovieByGenre(m , ip);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM