Results 1 to 6 of 6
Thread: NoSuchElementException
- 03-28-2012, 04:35 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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.
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.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(); } } }
**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~
- 03-28-2012, 05:02 PM #2
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!
- 03-28-2012, 07:29 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: NoSuchElementException
Last edited by Greyjoy; 03-28-2012 at 07:35 PM.
- 03-28-2012, 09:56 PM #4
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!
- 03-28-2012, 10:00 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: NoSuchElementException
I found a work around, so this is "Solved" now.
- 03-29-2012, 03:15 AM #6
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
-
Newbie JCreator NoSuchElementException Help!
By Sly Cooper in forum JCreatorReplies: 2Last Post: 12-18-2012, 05:44 PM -
NoSuchElementException
By senca in forum New To JavaReplies: 3Last Post: 04-26-2011, 05:47 PM -
java.util.NoSuchElementException
By Parkuss in forum New To JavaReplies: 1Last Post: 03-25-2011, 07:33 AM -
NoSuchElementException... help!!
By mpthegeneral in forum New To JavaReplies: 6Last Post: 03-19-2011, 11:34 PM -
nosuchelementexception
By nicaskdjf in forum New To JavaReplies: 1Last Post: 02-24-2011, 05:14 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks