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 2 of 2

Thread: NoSuchElementException(), simple program not behaving right with Scanner class.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default NoSuchElementException(), simple program not behaving right with Scanner class.

    So I am new to reading from files with Java and are trying to get this loop to keep reading from a file that the program has written to but it seems that every time I run the script the terminal seems to wait for me to input a value instead. And when I do just to see how it would behave I get a NoSuchElementException(). I'm not really sure what is happening but I know what that error means. It doesn't make sense to me why it is acting like that. It's been really driving me nuts.

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.File;
    import java.util.Scanner;
    import java.util.Random;
    public class BottleCapPrize
    {
        public static void main (String[] args) throws IOException
        {
            PrintWriter outFile = new PrintWriter(new File("BottleOpener.txt"));
            Scanner in = new Scanner(System.in);
            Scanner inFile = new Scanner(new File("BottleOpener.txt"));
            Random rand = new Random();
            int trials;
            int winCap = (int)(Math.random()*5)+1;
            int lenCaps = 0;
            int totalCaps = 0;
            double avg;
     
            System.out.print("Enter number of trials: ");
            trials = in.nextInt();
     
            for (int counter = 1; counter <= trials; counter++){
                while (rand.nextInt(5)+1 != winCap){
                    lenCaps++;
                }
                outFile.println(lenCaps);
                lenCaps = 0;
            }
            outFile.close ();
     
            while (in.hasNext()){
                totalCaps += inFile.nextInt();       //<- I GET THE ERROR HERE
            }
            System.out.println("AVG: ~" + (avg = (double)totalCaps/trials));
        }
    }
    Last edited by mwebb; October 25th, 2011 at 07:31 PM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: NoSuchElementException(), simple program not behaving right with Scanner class.

    Found it. while loop parameter is supposed to be (inFile.hasNext). Wow, I always seem to find the answer right after posting.

Similar Threads

  1. use Delimiter, Scanner Class
    By izzahmed in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 25th, 2011, 06:27 PM
  2. What's wrong with simple Scanner program?
    By SV25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 08:05 AM
  3. [SOLVED] If, else if, else statement not behaving as expected
    By techwiz24 in forum Loops & Control Statements
    Replies: 5
    Last Post: April 15th, 2011, 01:02 PM
  4. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 10:01 PM
  5. Replies: 1
    Last Post: May 13th, 2008, 09:08 AM