简体   繁体   中英

Java Scanner isn't reading my txt file

I am a tad frustrated because something this simple is giving me problems. It's been a while since I fired up some Java but I'm pretty sure I've done everything right. Here is my code:

import java.util.Scanner;

public class driver {
    public static void main(String[] args){
        Scanner input = new Scanner("numbers.txt");
        while(input.hasNext()){
            System.out.println(input.nextInt());
        }

    }
}

I tried moving the file to the src folder. I also tried pointing the Scanner to a file C://sample.txt but that didn't work either.

I'd appreciate any tips.

The Scanner(String) constructor points the Scanner at the contents of that string -- the string doesn't represent a filename. You can use new Scanner(new File("numbers.txt")) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM