Grant Clark Grant Clark - 1 month ago 14
Java Question

NoSuchElementException with tokenizer

for my project, i am supossed to read through a url file that is acting as a chart. my code should read through the file line by line, and also word by word with each line, but i am having trouble spliting the file into parts. i believe the problem is in the tokenizer

import java.util.Scanner;
import java.util.StringTokenizer;
public class SalaryReader {
public static void main (String[]args){

String firstName="blank";
String lastName="blank";
String rank;
String sal;
double salary=0.0;
double assocSalary= 0.0;
double fullSalary=0.0;
double assistSalary=0.0;
double facultySalary=0.0;
int assocCount=0;
int fullCount=0;
int assistCount=0;
int facultyCount=0;
try{
java.net.URL url = new java.net.URL("http://cs.armstrong.edu/liang/data/Salary.txt");
Scanner scan = new Scanner(url.openStream());

while (scan.hasNext()){
String line = scan.nextLine();
StringTokenizer t = new StringTokenizer(line, " ");
while(t.hasMoreTokens())
firstName=t.nextToken();
lastName=t.nextToken();
rank=t.nextToken();
sal= t.nextToken();
salary =Double.parseDouble(sal);
if (rank == "assistant"){
assistSalary=assistSalary+salary;
facultySalary=facultySalary+salary;
assistCount++;
facultyCount++;
}
if (rank=="associate"){
assocSalary=assocSalary+salary;
facultySalary=facultySalary+salary;
assocCount++;
facultyCount++;
}
if (rank=="full"){
fullSalary=fullSalary+salary;
facultySalary=facultySalary+salary;
fullCount++;
facultyCount++;
}
}
double fullAvg = fullSalary/fullCount;
double assocAvg = assocSalary/assocCount;
double assistAvg = assistSalary/assistCount;
double avg = facultySalary/facultyCount;

System.out.println("Full time salary: "+fullSalary+ " Full time average: "+fullAvg);
System.out.println("Associate salary: "+assocSalary+ " Associate average: "+assocAvg);
System.out.println("Assitant salary: "+assistSalary+ " Assistant average: "+assistAvg);
System.out.println("All faculty salary: "+facultySalary+ " average: "+avg);
}catch(java.net.MalformedURLException ex){
System.out.println("invalid URL");
}catch(java.io.IOException ex){
System.out.println("IO Errors");
}


}
}

Answer

You know, Java is not Python. As such, it uses braces ({ and }) to delimit code blocks (instead of indentation).

For instance:

while(t.hasMoreTokens())
    firstName=t.nextToken();
    lastName=t.nextToken();

means you'll be reading again and again the firstName while there are tokens available and then attempt to read the lastName in the conditions in which there are no more tokens.

Perhaps your intention was something on the line below:

        while(t.hasMoreTokens()) { // <- opening brace
            firstName=t.nextToken();
            lastName=t.nextToken();
            rank=t.nextToken();
            sal= t.nextToken();
            salary =Double.parseDouble(sal);
            if (rank.equals("assistant")){
              assistSalary=assistSalary+salary;
              facultySalary=facultySalary+salary;
              assistCount++;
              facultyCount++;
           }
           if (rank.equals("associate")){
             assocSalary=assocSalary+salary;
             facultySalary=facultySalary+salary;
             assocCount++;
             facultyCount++;
           }
           if (rank.equals("full")){
             fullSalary=fullSalary+salary;
             facultySalary=facultySalary+salary;
             fullCount++;
             facultyCount++;
           }
       } // <- "while cycle" closing brace.