Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What am i doing wrong?! It prints to the cosole all of the city names correctly

ID: 3538837 • Letter: W

Question

What am i doing wrong?! It prints to the cosole all of the city names correctly but it prints all the abbreviations as null! But I when I use bufferedReader instead of scanner it works fine?! The problem is i have to use scanner class for my class


public void loadFile(File fileCity,File fileAbbrev){
    int i=0;
    int number =0;
try{
   
    Scanner city_reader = new Scanner(new FileReader(fileCity.getName()));
    Scanner abbrev_reader = new Scanner(new FileReader(fileAbbrev.getName()));
    String str;
    number = Integer.parseInt(abbrev_reader.nextLine());

cities = new String[number];
abbreviations = new String[number];;
route = new String[number];
distances = new int[number][number];

while ((str = city_reader.nextLine()) != null)

{
cities[i++] = str;
}
i=0;
while ((str = abbrev_reader.nextLine()) != null)
{
abbreviations[i++] = str;
}
}catch (Exception e){//Catch exception if any

//System.err.println("Error: " + e.getMessage()); }
}
// PRINT THOSE HERE:
System.out.println("City Name Abbreviation [" + number + "] cities.");
System.out.println("------------- ------------ ");
for(i=0; i<number;i++)
System.out.println(cities[i] + " " + abbreviations[i]);
}



Explanation / Answer

Scanner city_reader = new Scanner(new FileReader(fileCity.getName()));

Scanner abbrev_reader = new Scanner(new FileReader(fileAbbrev.getName()));



change new FileReader to fileCity , ,FileAbbrev respectively

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote