Results 1 to 6 of 6
  1. #1
    Greyjoy is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default NoSuchElementException

    Hi, I'm having trouble with some code, and I can't figure out why it won't run correctly. I've done all the debugging I could think of and I've narrowed the problem down to one loop, but I'm not sure why it won't work.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package cis;
    
    /**
     *
     * @author Porksberry
     */
    import java.io.File;
    import java.util.*;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class Concordances 
    {
        public static void main(String[]args)throws FileNotFoundException
        {
            File input = new File("input.txt");
            Scanner fileScanner = new Scanner(input);
            String HuckFinn = fileScanner.nextLine();
    
            while(fileScanner.hasNextLine())
            {
                HuckFinn = HuckFinn + fileScanner.nextLine();
            }
            
    
            int count = 0;
            String[] arr = HuckFinn.split(" ");
            
            for(int i=0;i<arr.length;i++)
            {
                if(arr[i].equals(" "))
                {
                    
                }
                else
                {
                    count++;
                }
            }
            int NumWords = count;
            System.out.println(NumWords);
    
            Scanner FS = new Scanner(HuckFinn);
            System.out.println("Creating wordArray...");
            String wordArray[] = new String[NumWords];
           
    
            for(int i=0;i<=NumWords;i++)
            {
                wordArray[i]=FS.next();
            }
        
    
        
        }
        
        
        
       
        
        
       
    }
    The for loop at the very bottom is where I believe the problems to be occurring, but I'm still fairly new to coding and I just can't see what's wrong with it.

    **When this code is run, I get:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1347)
    at cis.Concordances.main(Concordances.java:51)
    Java Result: 1

    Any help much appreciated~

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: NoSuchElementException

    Print out the elements in that loop as you read them in. What is the last element you successfully read?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Greyjoy is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: NoSuchElementException

    Quote Originally Posted by KevinWorkman View Post
    Print out the elements in that loop as you read them in. What is the last element you successfully read?
    NoSuchElementException-ss1.png

    The program reads all of the thousands of words in the text, yet when it's almost at the end of the file, this appears in between some words. I have no idea how to fix this.



    Disregard the second picture, I just can't take it out.
    Attached Thumbnails Attached Thumbnails NoSuchElementException-ss.png  
    Last edited by Greyjoy; 03-28-2012 at 07:35 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: NoSuchElementException

    How many words are there? How many times do you loop? Are you sure? Hint- if I loop from 0 through 100, I'm actually looping 101 times.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Greyjoy is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: NoSuchElementException

    I found a work around, so this is "Solved" now.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: NoSuchElementException

    What was the workaround? You shouldn't need one...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Newbie JCreator NoSuchElementException Help!
    By Sly Cooper in forum JCreator
    Replies: 2
    Last Post: 12-18-2012, 05:44 PM
  2. NoSuchElementException
    By senca in forum New To Java
    Replies: 3
    Last Post: 04-26-2011, 05:47 PM
  3. java.util.NoSuchElementException
    By Parkuss in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 07:33 AM
  4. NoSuchElementException... help!!
    By mpthegeneral in forum New To Java
    Replies: 6
    Last Post: 03-19-2011, 11:34 PM
  5. nosuchelementexception
    By nicaskdjf in forum New To Java
    Replies: 1
    Last Post: 02-24-2011, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •