Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Using inputFile.hasNext ?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using inputFile.hasNext ?

    I don't know why my code isn't working. It's worked before but I don't know why this time it isn't.
                public static void inputData() throws IOException{
                    Scanner keyboard = new Scanner(System.in);
                    String input;
                    int lines;
                    System.out.println("Please enter a file name: ");
                    input = keyboard.nextLine();
                    File myfile = new File(input);
                    if (!myfile.exists()){
                            System.out.println("File not found.");
                            System.exit(0);
                    }
                    Scanner inputFile = new Scanner(myfile);
                    lines = inputFile.nextInt();
                    System.out.println(lines);
                    int[] array = new int[lines];
                    while (inputFile.hasNext()){
                            for (int i=0; i<=lines; i++){
                                    int moreLines = inputFile.nextInt();
                                    array[i] = moreLines;
                            }
                    }
                    System.out.println(array[3]);
                    inputFile.close();
                }

    Here's what the error returns:
        Exception in thread "main" java.util.NoSuchElementException
            at java.util.Scanner.throwFor(Scanner.java:838)
            at java.util.Scanner.next(Scanner.java:1461)
            at java.util.Scanner.nextInt(Scanner.java:2091)
            at java.util.Scanner.nextInt(Scanner.java:2050)
            at Paniagua_ArrayProcessing.inputData(Paniagua_ArrayProcessing.java:24)
            at Paniagua_ArrayProcessing.main(Paniagua_ArrayProcessing.java:5)
    Last edited by deejaypanininini; March 15th, 2014 at 05:42 PM. Reason: Including error

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Using inputFile.hasNext ?

    The code is expecting a number of ints in the file, but it's finding something else, something that can't be an int. If Scanner specifies the type of data being sought, as in nextInt(), that type of data must be found. Either correct the code to scan for the type of data it will find in the file, OR correct the file to have the type of data the code is looking for.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    deejaypanininini (March 15th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using inputFile.hasNext ?

    The file contains 5 numbers: 0, 1, 5, -9, and -10. Aren't these all in the int range?

    Edit: fixed it. I moved my for loop out and changed the while loop into an if statement and put it in the for loop. All's better for now.
    Last edited by deejaypanininini; March 15th, 2014 at 06:10 PM.

Similar Threads

  1. what is this error in hasnext in webgraph
    By javakar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 7th, 2014, 04:48 PM
  2. Big problems with my hasNext method and outer loop! Please help!
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 26th, 2012, 08:55 AM

Tags for this Thread