1

问题是输出每个级别的总工资和平均工资,这是我的代码:

     URL url=new URL("http://liveexample.pearsoncmg.com/data/Salary.txt");
        Scanner input=new Scanner(url.openStream());
        String[]FirstName=new String[1000];
        String[]LastName=new String[1000];
        String[]rank=new String[1000];
        int[]salary=new int[1000];
        
        int i=0;
        int count=0;
        double sum=0;
        while(input.hasNext()) {
            FirstName[i]=input.nextLine();
            LastName[i]=input.nextLine();
            rank[i]=input.nextLine();
            salary[i]=input.nextInt();
            if(rank[i]=="assistant") {
                count++;
                sum+=salary[i];
                System.out.print("Total salary of assistant professors:"+sum+" average: "+sum/count);
            }
            else if(rank[i]=="associate") {
                count++;
                sum+=salary[i];
                System.out.print("Total salary of associate professors:"+sum+" average: "+sum/count);
            }
            else if(rank[i]=="full") {
                count++;
                sum+=salary[i];
                System.out.print("Total salary of full professors:"+sum+" average: "+sum/count);
            }
            else if(rank[i]=="faculty") {
                count++;
                sum+=salary[i];
                System.out.print("Total salary of faculty professors:"+sum+" average: "+sum/count);
            }
            input.close();
        }

但是输出是 InputMismatchException,我检查了很多次代码,我找不到错误

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.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at HW11_09156136.HW11_09156136_01.main(HW11_09156136_01.java:24)
4

2 回答 2

1

input.nextLine()不检索流中的下一个单词,它检索下一行。根据您在程序中提供的 url,您的代码将分配FirstName1 LastName1 assistant 79174.73FirstName[i]FirstName2 LastName2 associate 70817.75toLastName[i]等,直到到达 line FirstName4 LastName4 full 116992.43。从那里它尝试解析FirstName4int,这当然失败了。您需要使用您的程序next(),它只会将文本获取到下一个空格,而不是换行符,所以

            FirstName[i]=input.nextLine();
            LastName[i]=input.nextLine();
            rank[i]=input.nextLine();
            salary[i]=input.nextInt();

可以用类似的东西代替

            FirstName[i]=input.next();
            LastName[i]=input.next();
            rank[i]=input.next();
            salary[i]=input.nextInt();
            input.nextLine();

(请注意,input.nextLine()最后你需要一个额外的,推理在这里看到)

于 2021-05-28T02:43:25.103 回答
-1
FirstName7 LastName7 assistant 70071.81

您的输入行包含一位教授的所有信息,因此您必须一次读取一位教授的一行。你可以做这样的事情。此外,您必须将不同等级的数据存储在不同的变量集中counting and sum.

    URL url=new URL("http://liveexample.pearsoncmg.com/data/Salary.txt");
    Scanner input=new Scanner(url.openStream());
    String[]FirstName=new String[1000];
    String[]LastName=new String[1000];
    String[]rank=new String[1000];
    int[]salary=new int[1000];
    
    int i=0;//indexing
    
    //sums of total number of professors according to there rank
    int count_assistant=0;
    int count_associate=0;
    int count_full=0;
    int count_faculty=0;
    
    //sum up of salary of professors according to there rank
    double sum_assistant=0;
    double sum_associate=0;
    double sum_full=0;
    double sum_faculty=0;
    
    String data[] = null;
    while(input.hasNextLine()) {
        data = input.nextLine().split(" ");
        FirstName[i] = data[0];
        LastName[i] = data[1];
        rank[i] = data[2];
        salary[i] = Double.parseDouble(data[3]);
        if(rank[i]=="assistant") {
            count_assistant++;
            sum_assistant+=salary[i];
        }
        else if(rank[i]=="associate") {
            count_associate++;
            sum_associate+=salary[i];
        }
        else if(rank[i]=="full") {
            count_full++;
            sum_full+=salary[i];
        }
        else if(rank[i]=="faculty") {
            count_faculty++;
            sum_faculty+=salary[i];
        }
        i++;//increment for next professor
    }
    input.close();
    System.out.print("Total salary of assistant professors:"+sum_assistant+" average: "+sum_assistant/count_assistant);
    System.out.print("Total salary of associate professors:"+sum_associate+" average: "+sum_associate/count_associate);
    System.out.print("Total salary of full professors:"+sum_full+" average: "+sum_full/count_full);
    System.out.print("Total salary of faculty professors:"+sum_faculty+" average: "+sum_faculty/count_faculty);
}
于 2021-05-28T03:18:48.440 回答