问题是输出每个级别的总工资和平均工资,这是我的代码:
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)