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

why isn\'t this working? import java.io.File; import java.io.FileNotFoundExcepti

ID: 646964 • Letter: W

Question

why isn't this working?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;


public class Reverser {

   public static void main(String[] args) {
      
       File f = new File("C:\Users\Nick\Desktop\wordlist.txt");
       Scanner read = null;
      
       try {
           read = new Scanner(f);
       } catch (FileNotFoundException e) {
          
           e.printStackTrace();
       }
      
       try{
       while(read.hasNext()){
           System.out.println(read.nextInt());
       }
       }
       catch(NoSuchElementException e){
           e.printStackTrace();
       }
      
       //System.out.println("Still executing");
   }
}

THE COMMAND LINE READS:

java.util.InputMismatchException
   at java.util.Scanner.throwFor(Scanner.java:864)
   at java.util.Scanner.next(Scanner.java:1485)
   at java.util.Scanner.nextInt(Scanner.java:2117)
   at java.util.Scanner.nextInt(Scanner.java:2076)
   at Reverser.main(Reverser.java:23)

Explanation / Answer

here you are doing mistake

new Scanner(f);

do it like

new Scanner(newFileInputStream(f));