1

The question is to output the total salary and average salary of each rank, this is my code:

     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();
        }

however the output is InputMismatchException,and i checked my code many times, i can't find the error

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)
Dice Highe
  • 13
  • 4
  • You fix it by not supplying mismatched input. If you want to detect that and tell the user to retry you need to use `hasNextInt()` and friends. You haven't shown what the input looks like but maybe you need to call `nextLine()` after `nextInt()`. – user207421 May 28 '21 at 02:15
  • 1
    Since you didn't show the data you're reading this question cannot be answered. Anyway, this code has several issues. Read all three questions to fix them: [Scanner is skipping nextLine() after using next() or nextFoo()?](//stackoverflow.com/q/13102045), [java.util.NoSuchElementException - Scanner reading user input](//stackoverflow.com/q/13042008) and [How do I compare strings in Java?](//stackoverflow.com/q/513832) – Tom May 28 '21 at 02:27

2 Answers2

1

input.nextLine() doesn't retrieve the next word in the stream, it retrieves the next line. Based on the url you provided in the program, your code is assigning FirstName1 LastName1 assistant 79174.73 to FirstName[i], FirstName2 LastName2 associate 70817.75 to LastName[i], etc. until it gets to the line FirstName4 LastName4 full 116992.43. From there it tries to parse FirstName4 as an int, which of course fails. You need your program to use next(), which only gets text up to the next space, not newline, so

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

could be replaced with something like

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

(Note that you need an extra input.nextLine() at the end, reasoning seen here)

Nosrep
  • 485
  • 6
  • 16
-1
FirstName7 LastName7 assistant 70071.81

Your input line has all information for a single professor so you have to read one line at a time for a single professor. You can do something like this. Also, you have to store data for a different rank in different sets of variables both for 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);
}
Ashish Mishra
  • 170
  • 1
  • 14
  • It can be done much more simply without `String.split()` and all the extra variables. See the answer by Nosrep. – user207421 May 28 '21 at 03:26
  • I agree with the split() but the user code needs to sum up different ranks professor count and salary count how will you do that without variables? As it was already added by Nosrep i thought a different approach – Ashish Mishra May 28 '21 at 03:28