I cant seem to find the problem with this program I am building as a learning exercise. I am getting errors referenced to extremely simple pieces of code. In the output the first thing that should be printed is the random double number that the program selects then it should print the int conversion of that number. Then it should print the word that was selected from a file and the number of words in the file(29).

Also is there a more efficient way to count the words in a file? If so how?


Here is the code:
note: Line numbers are matched correctly

import java.util.Scanner;
import java.io.File;

public class HangmanFile33 {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        
        Scanner inputEa = new Scanner(new File("9_33Easy.txt"));
        Scanner inputMe = new Scanner(new File("9_33Medium.txt"));
        Scanner inputHa = new Scanner(new File("9_33Hard.txt"));
        Scanner inputEx = new Scanner(new File("9_33Extreme.txt"));
        
        String word;
        
        System.out.println("Press 0 to exit \nor \nEnter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): ");
        int Diff = input.nextInt();
        
        if(Diff == '0')
            System.exit(0);
        else if(Diff == '1') {
            word = PickWord(inputEa);
        }
        else if(Diff == '2') {
            word = PickWord(inputMe);
        }
        else if(Diff == '3') {
            word = PickWord(inputHa);
        }
        else if(Diff == '4') {
            word = PickWord(inputEx);
        }
        else {
            System.out.println("Invalid Difficulity. \n Please try again.");
        }
    }
    public static String PickWord(Scanner s){
        String word;
        double randomDouble = Math.random() * CountWords(s);
        System.out.println(randomDouble);
        int randomInt = (int)randomDouble;
        System.out.println(randomInt);
        for(int i = 0; i < randomInt; i++) {
            s.next();
        }
        word = s.next();
        System.out.println(word);
        System.out.println(CountWords(s));
        return word;
    }
    public static int CountWords(Scanner s) {
        int wordCount = 0;
        while(s.hasNext()) {
            s.next();
            wordCount++;           
        }
        return wordCount;
    }
}

And here is the run of the program:

Press 0 to exit 
or 
Enter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): 
1
Exception in thread "main" java.util.NoSuchElementException
54
        at java.util.Scanner.skip(Scanner.java:1706)
        at java.util.Scanner.skip(Scanner.java:1723)
        at ProgrammingExcercises.Ch09.HangmanFile33.PickWord(HangmanFile33.java:53)
        at ProgrammingExcercises.Ch09.HangmanFile33.main(HangmanFile33.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

And here is the program run in debug mode:

Adding reference: ant.PropertyHelper
Detected Java version: 1.6 in: C:\Program Files (x86)\Java\jdk1.6.0_20\jre
Detected OS: Windows Vista
Adding reference: ant.ComponentHelper
Trying to override old definition of task java
 +Datatype java org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml from a zip file
parsing buildfile jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
 +Datatype nbbrowse org.netbeans.modules.ant.browsetask.NbBrowse
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-ant-browsetask.jar!/org/netbeans/modules/ant/browsetask/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.ant.browsetask:nbbrowse org.netbeans.modules.ant.browsetask.NbBrowse
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-web-client-javascript-debugger-ant.jar!/org/netbeans/modules/web/client/javascript/debugger/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-web-client-javascript-debugger-ant.jar!/org/netbeans/modules/web/client/javascript/debugger/ant/antlib.xml from a zip file
 +Datatype nbjsdebugstart org.netbeans.modules.web.client.javascript.debugger.ant.NbJsDebugStart
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-web-client-javascript-debugger-ant.jar!/org/netbeans/modules/web/client/javascript/debugger/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-web-client-javascript-debugger-ant.jar!/org/netbeans/modules/web/client/javascript/debugger/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.web.client.javascript.debugger.ant:nbjsdebugstart org.netbeans.modules.web.client.javascript.debugger.ant.NbJsDebugStart
parsing buildfile jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-profiler.jar!/org/netbeans/modules/profiler/antlib.xml with URI = jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-profiler.jar!/org/netbeans/modules/profiler/antlib.xml from a zip file
 +Datatype nbprofileattach org.netbeans.modules.profiler.ant.NBProfileAttachTask
 +Datatype nbprofiledirect org.netbeans.modules.profiler.ant.NBProfileDirectTask
parsing buildfile jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-profiler.jar!/org/netbeans/modules/profiler/antlib.xml with URI = jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-profiler.jar!/org/netbeans/modules/profiler/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.profiler:nbprofileattach org.netbeans.modules.profiler.ant.NBProfileAttachTask
 +Datatype antlib:org.netbeans.modules.profiler:nbprofiledirect org.netbeans.modules.profiler.ant.NBProfileDirectTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/apisupport/ant/nblib/org-netbeans-modules-apisupport-ant.jar!/org/netbeans/modules/apisupport/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/apisupport/ant/nblib/org-netbeans-modules-apisupport-ant.jar!/org/netbeans/modules/apisupport/ant/antlib.xml from a zip file
 +Datatype nbinstaller org.netbeans.modules.apisupport.ant.InstallModuleTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/apisupport/ant/nblib/org-netbeans-modules-apisupport-ant.jar!/org/netbeans/modules/apisupport/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/apisupport/ant/nblib/org-netbeans-modules-apisupport-ant.jar!/org/netbeans/modules/apisupport/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.apisupport.ant:nbinstaller org.netbeans.modules.apisupport.ant.InstallModuleTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-mobility-project-ant.jar!/org/netbeans/modules/mobility/project/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-mobility-project-ant.jar!/org/netbeans/modules/mobility/project/ant/antlib.xml from a zip file
 +Datatype nb-mobility-debug org.netbeans.modules.mobility.project.ant.KdpDebugTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-mobility-project-ant.jar!/org/netbeans/modules/mobility/project/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-mobility-project-ant.jar!/org/netbeans/modules/mobility/project/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.mobility.project.ant:nb-mobility-debug org.netbeans.modules.mobility.project.ant.KdpDebugTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-deployment-wm.jar!/org/netbeans/modules/deployment/wm/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-deployment-wm.jar!/org/netbeans/modules/deployment/wm/antlib.xml from a zip file
 +Datatype wmDeploy org.netbeans.modules.deployment.wm.WindowsMobileDeployTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-deployment-wm.jar!/org/netbeans/modules/deployment/wm/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/mobility/ant/nblib/org-netbeans-modules-deployment-wm.jar!/org/netbeans/modules/deployment/wm/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.deployment.wm:wmDeploy org.netbeans.modules.deployment.wm.WindowsMobileDeployTask
parsing buildfile jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-loadgenerator.jar!/org/netbeans/modules/loadgenerator/antlib.xml with URI = jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-loadgenerator.jar!/org/netbeans/modules/loadgenerator/antlib.xml from a zip file
 +Datatype loadgenstart org.netbeans.modules.loadgenerator.ant.StartLoadgenTask
parsing buildfile jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-loadgenerator.jar!/org/netbeans/modules/loadgenerator/antlib.xml with URI = jar:file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/ant/nblib/org-netbeans-modules-loadgenerator.jar!/org/netbeans/modules/loadgenerator/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.loadgenerator:loadgenstart org.netbeans.modules.loadgenerator.ant.StartLoadgenTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-debugger-jpda-ant.jar!/org/netbeans/modules/debugger/jpda/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-debugger-jpda-ant.jar!/org/netbeans/modules/debugger/jpda/ant/antlib.xml from a zip file
 +Datatype nbjpdaconnect org.netbeans.modules.debugger.jpda.ant.JPDAConnect
 +Datatype nbjpdastart org.netbeans.modules.debugger.jpda.ant.JPDAStart
 +Datatype sourcepath org.netbeans.modules.debugger.jpda.ant.JPDAStart$Sourcepath
 +Datatype nbjpdareload org.netbeans.modules.debugger.jpda.ant.JPDAReload
 +Datatype nbjpdaappreloaded org.netbeans.modules.debugger.jpda.ant.JPDAAppReloaded
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-debugger-jpda-ant.jar!/org/netbeans/modules/debugger/jpda/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-debugger-jpda-ant.jar!/org/netbeans/modules/debugger/jpda/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.debugger.jpda.ant:nbjpdaconnect org.netbeans.modules.debugger.jpda.ant.JPDAConnect
 +Datatype antlib:org.netbeans.modules.debugger.jpda.ant:nbjpdastart org.netbeans.modules.debugger.jpda.ant.JPDAStart
 +Datatype antlib:org.netbeans.modules.debugger.jpda.ant:sourcepath org.netbeans.modules.debugger.jpda.ant.JPDAStart$Sourcepath
 +Datatype antlib:org.netbeans.modules.debugger.jpda.ant:nbjpdareload org.netbeans.modules.debugger.jpda.ant.JPDAReload
 +Datatype antlib:org.netbeans.modules.debugger.jpda.ant:nbjpdaappreloaded org.netbeans.modules.debugger.jpda.ant.JPDAAppReloaded
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-j2ee-ant.jar!/org/netbeans/modules/j2ee/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-j2ee-ant.jar!/org/netbeans/modules/j2ee/ant/antlib.xml from a zip file
 +Datatype nbdeploy org.netbeans.modules.j2ee.ant.Deploy
 +Datatype nbundeploy org.netbeans.modules.j2ee.ant.Undeploy
 +Datatype nbverify org.netbeans.modules.j2ee.ant.Verify
 +Datatype nbstartserver org.netbeans.modules.j2ee.ant.StartServer
 +Datatype nbstartprofiledserver org.netbeans.modules.j2ee.ant.StartProfiledServer
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-j2ee-ant.jar!/org/netbeans/modules/j2ee/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/enterprise/ant/nblib/org-netbeans-modules-j2ee-ant.jar!/org/netbeans/modules/j2ee/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.j2ee.ant:nbdeploy org.netbeans.modules.j2ee.ant.Deploy
 +Datatype antlib:org.netbeans.modules.j2ee.ant:nbundeploy org.netbeans.modules.j2ee.ant.Undeploy
 +Datatype antlib:org.netbeans.modules.j2ee.ant:nbverify org.netbeans.modules.j2ee.ant.Verify
 +Datatype antlib:org.netbeans.modules.j2ee.ant:nbstartserver org.netbeans.modules.j2ee.ant.StartServer
 +Datatype antlib:org.netbeans.modules.j2ee.ant:nbstartprofiledserver org.netbeans.modules.j2ee.ant.StartProfiledServer
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-javawebstart.jar!/org/netbeans/modules/javawebstart/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-javawebstart.jar!/org/netbeans/modules/javawebstart/antlib.xml from a zip file
 +Datatype jnlpsignjars org.netbeans.modules.javawebstart.SignJarsTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-javawebstart.jar!/org/netbeans/modules/javawebstart/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-javawebstart.jar!/org/netbeans/modules/javawebstart/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.javawebstart:jnlpsignjars org.netbeans.modules.javawebstart.SignJarsTask
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-java-source-ant.jar!/org/netbeans/modules/java/source/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-java-source-ant.jar!/org/netbeans/modules/java/source/ant/antlib.xml from a zip file
Trying to override old definition of task javac
 +Datatype javac org.netbeans.modules.java.source.ant.JavacTask
 +Datatype translate-classpath org.netbeans.modules.java.source.ant.TranslateClassPath
parsing buildfile jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-java-source-ant.jar!/org/netbeans/modules/java/source/ant/antlib.xml with URI = jar:file:/C:/Program%20Files%20(x86)/NetBeans%206.9/java/ant/nblib/org-netbeans-modules-java-source-ant.jar!/org/netbeans/modules/java/source/ant/antlib.xml from a zip file
 +Datatype antlib:org.netbeans.modules.java.source.ant:javac org.netbeans.modules.java.source.ant.JavacTask
 +Datatype antlib:org.netbeans.modules.java.source.ant:translate-classpath org.netbeans.modules.java.source.ant.TranslateClassPath
Setting ro project property: ant.file -> C:\Users\Justin VanDeBrake\.netbeans\6.9\var\cache\executor-snippets\run.xml
Setting ro project property: ant.version -> Apache Ant version 1.8.1 compiled on April 30 2010
Setting ro project property: ant.home -> C:\Users\Justin VanDeBrake\.netbeans\6.9\ant
Setting ro project property: platform.java -> C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe
Setting ro project property: run.jvmargs -> 
Setting ro project property: platform.bootcp -> C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\sunrsasign.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\classes;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.6.0_20\jre\lib\ext\sunpkcs11.jar
Setting ro project property: classname -> ProgrammingExcercises.Ch09.HangmanFile33
Setting ro project property: encoding -> UTF-8
Setting ro project property: build.compiler.emacs -> true
Setting ro project property: classpath -> C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-api-6.0.jar;C:\Program Files\sges-v3\javadb\lib\derby.jar;C:\Program Files\sges-v3\javadb\lib\derbyclient.jar;C:\Program Files\sges-v3\javadb\lib\derbynet.jar;C:\Program Files\sges-v3\glassfish\modules\bean-validator.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.auth.message.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\webservices-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\jaxb-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jar;C:\Program Files\sges-v3\glassfish\modules\jsr311-api.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.jacc.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-api.jar;C:\Program Files\sges-v3\glassfish\modules\jstl-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.management.j2ee.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\javax.annotation.jar;C:\Program Files\sges-v3\glassfish\modules\javax.persistence.jar;C:\Program Files\sges-v3\glassfish\modules\javax.transaction.jar;C:\Program Files\sges-v3\glassfish\modules\javax.enterprise.deploy.jar;C:\Program Files\sges-v3\glassfish\modules\javax.jms.jar;C:\Program Files\sges-v3\glassfish\modules\weld-osgi-bundle.jar;C:\Program Files\sges-v3\glassfish\modules\javax.ejb.jar;C:\Program Files\sges-v3\glassfish\modules\javax.resource.jar;C:\Program Files\sges-v3\glassfish\modules\mail.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jstl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jar;C:\Program Files\sges-v3\glassfish\modules\web-core.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\javax.annotation.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\jaxb-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\webservices-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-agui-swing-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\ant\nblib\org-netbeans-modules-j2me-cdc-project-nsicom.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dcore.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dutils.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\vecmath.jar;C:\Java_lib\Java 3d lib\joalmixer-1_5_2\joalmixer-1_5_2\joalmixer.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\ext\javac-api-nb-7.0-b07.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-pp-awt-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\org-netbeans-modules-mobility-antext.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\org-netbeans-modules-classfile.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-web-api-6.0.jar;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\build\classes
Setting ro project property: work.dir -> C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile C:\Users\Justin VanDeBrake\.netbeans\6.9\var\cache\executor-snippets\run.xml with URI = file:/C:/Users/Justin%20VanDeBrake/.netbeans/6.9/var/cache/executor-snippets/run.xml
Setting ro project property: ant.project.name -> {0} (run)
Adding reference: {0} (run)
Setting ro project property: ant.project.default-target -> run
Setting ro project property: ant.file.{0} (run) -> C:\Users\Justin VanDeBrake\.netbeans\6.9\var\cache\executor-snippets\run.xml
Setting ro project property: ant.file.type.{0} (run) -> file
Project base dir set to: C:\Users\Justin VanDeBrake\.netbeans\6.9\var\cache\executor-snippets
 +Target: 
 +Target: run
Adding reference: ant.LocalProperties
Setting ro project property: ant.project.invoked-targets -> run
Attempting to create object of type org.apache.tools.ant.helper.DefaultExecutor
Adding reference: ant.executor
Build sequence for target(s) `run' is [run]
Complete build sequence is [run, ]
run:
Setting project property: classpath-translated -> C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-api-6.0.jar;C:\Program Files\sges-v3\javadb\lib\derby.jar;C:\Program Files\sges-v3\javadb\lib\derbyclient.jar;C:\Program Files\sges-v3\javadb\lib\derbynet.jar;C:\Program Files\sges-v3\glassfish\modules\bean-validator.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.auth.message.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\webservices-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\jaxb-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jar;C:\Program Files\sges-v3\glassfish\modules\jsr311-api.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.jacc.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-api.jar;C:\Program Files\sges-v3\glassfish\modules\jstl-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.management.j2ee.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\javax.annotation.jar;C:\Program Files\sges-v3\glassfish\modules\javax.persistence.jar;C:\Program Files\sges-v3\glassfish\modules\javax.transaction.jar;C:\Program Files\sges-v3\glassfish\modules\javax.enterprise.deploy.jar;C:\Program Files\sges-v3\glassfish\modules\javax.jms.jar;C:\Program Files\sges-v3\glassfish\modules\weld-osgi-bundle.jar;C:\Program Files\sges-v3\glassfish\modules\javax.ejb.jar;C:\Program Files\sges-v3\glassfish\modules\javax.resource.jar;C:\Program Files\sges-v3\glassfish\modules\mail.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jstl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jar;C:\Program Files\sges-v3\glassfish\modules\web-core.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\javax.annotation.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\jaxb-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\webservices-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-agui-swing-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\ant\nblib\org-netbeans-modules-j2me-cdc-project-nsicom.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dcore.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dutils.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\vecmath.jar;C:\Java_lib\Java 3d lib\joalmixer-1_5_2\joalmixer-1_5_2\joalmixer.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\ext\javac-api-nb-7.0-b07.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-pp-awt-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\org-netbeans-modules-mobility-antext.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\org-netbeans-modules-classfile.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-web-api-6.0.jar;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\build\classes;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\src
Override ignored for property "run.jvmargs"
Override ignored for property "work.dir"
Setting project property: application.args -> 
Executing 'C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe' with arguments:
'-Dfile.encoding=UTF-8'
'-classpath'
'C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-api-6.0.jar;C:\Program Files\sges-v3\javadb\lib\derby.jar;C:\Program Files\sges-v3\javadb\lib\derbyclient.jar;C:\Program Files\sges-v3\javadb\lib\derbynet.jar;C:\Program Files\sges-v3\glassfish\modules\bean-validator.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.auth.message.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\webservices-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\jaxb-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jar;C:\Program Files\sges-v3\glassfish\modules\jsr311-api.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.jacc.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-api.jar;C:\Program Files\sges-v3\glassfish\modules\jstl-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.management.j2ee.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\javax.annotation.jar;C:\Program Files\sges-v3\glassfish\modules\javax.persistence.jar;C:\Program Files\sges-v3\glassfish\modules\javax.transaction.jar;C:\Program Files\sges-v3\glassfish\modules\javax.enterprise.deploy.jar;C:\Program Files\sges-v3\glassfish\modules\javax.jms.jar;C:\Program Files\sges-v3\glassfish\modules\weld-osgi-bundle.jar;C:\Program Files\sges-v3\glassfish\modules\javax.ejb.jar;C:\Program Files\sges-v3\glassfish\modules\javax.resource.jar;C:\Program Files\sges-v3\glassfish\modules\mail.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jstl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jar;C:\Program Files\sges-v3\glassfish\modules\web-core.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\javax.annotation.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\jaxb-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\webservices-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-agui-swing-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\ant\nblib\org-netbeans-modules-j2me-cdc-project-nsicom.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dcore.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dutils.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\vecmath.jar;C:\Java_lib\Java 3d lib\joalmixer-1_5_2\joalmixer-1_5_2\joalmixer.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\ext\javac-api-nb-7.0-b07.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-pp-awt-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\org-netbeans-modules-mobility-antext.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\org-netbeans-modules-classfile.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-web-api-6.0.jar;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\build\classes;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\src'
'ProgrammingExcercises.Ch09.HangmanFile33'
The ' characters around the executable and arguments are
not part of the command.
Execute:Java13CommandLauncher: Executing 'C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe' with arguments:
'-Dfile.encoding=UTF-8'
'-classpath'
'C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-api-6.0.jar;C:\Program Files\sges-v3\javadb\lib\derby.jar;C:\Program Files\sges-v3\javadb\lib\derbyclient.jar;C:\Program Files\sges-v3\javadb\lib\derbynet.jar;C:\Program Files\sges-v3\glassfish\modules\bean-validator.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.auth.message.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\webservices-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\jaxb-api-osgi.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jar;C:\Program Files\sges-v3\glassfish\modules\jsr311-api.jar;C:\Program Files\sges-v3\glassfish\modules\javax.security.jacc.jar;C:\Program Files\sges-v3\glassfish\modules\jsf-api.jar;C:\Program Files\sges-v3\glassfish\modules\jstl-impl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.management.j2ee.jar;C:\Program Files\sges-v3\glassfish\modules\endorsed\javax.annotation.jar;C:\Program Files\sges-v3\glassfish\modules\javax.persistence.jar;C:\Program Files\sges-v3\glassfish\modules\javax.transaction.jar;C:\Program Files\sges-v3\glassfish\modules\javax.enterprise.deploy.jar;C:\Program Files\sges-v3\glassfish\modules\javax.jms.jar;C:\Program Files\sges-v3\glassfish\modules\weld-osgi-bundle.jar;C:\Program Files\sges-v3\glassfish\modules\javax.ejb.jar;C:\Program Files\sges-v3\glassfish\modules\javax.resource.jar;C:\Program Files\sges-v3\glassfish\modules\mail.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jsp.jstl.jar;C:\Program Files\sges-v3\glassfish\modules\javax.servlet.jar;C:\Program Files\sges-v3\glassfish\modules\web-core.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\javax.annotation.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\jaxb-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee6-endorsed\webservices-api-osgi.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-agui-swing-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\ant\nblib\org-netbeans-modules-j2me-cdc-project-nsicom.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dcore.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\j3dutils.jar;C:\Java_lib\Java 3d lib\j3d-1_5_2-windows-amd64\j3d-1_5_2-windows-amd64\j3d-jre\lib\ext\vecmath.jar;C:\Java_lib\Java 3d lib\joalmixer-1_5_2\joalmixer-1_5_2\joalmixer.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\ext\javac-api-nb-7.0-b07.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\ext\cdc-pp-awt-layout.jar;C:\Program Files (x86)\NetBeans 6.9\mobility\modules\org-netbeans-modules-mobility-antext.jar;C:\Program Files (x86)\NetBeans 6.9\java\modules\org-netbeans-modules-classfile.jar;C:\Program Files (x86)\NetBeans 6.9\enterprise\modules\ext\javaee-web-api-6.0.jar;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\build\classes;C:\Users\Justin VanDeBrake\Documents\NetBeansProjects\Intro to Java\src'
'ProgrammingExcercises.Ch09.HangmanFile33'
The ' characters around the executable and arguments are
not part of the command.
Press 0 to exit 
or 
Enter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): 
1
Exception in thread "main" java.util.NoSuchElementException
54
        at java.util.Scanner.skip(Scanner.java:1706)
        at java.util.Scanner.skip(Scanner.java:1723)
        at ProgrammingExcercises.Ch09.HangmanFile33.PickWord(HangmanFile33.java:53)
        at ProgrammingExcercises.Ch09.HangmanFile33.main(HangmanFile33.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Could not load class (org.apache.tools.ant.taskdefs.repository.MavenRepository) for type mavenrepository
Could not load a dependent class (jdepend/xmlui/JDepend) for type jdepend
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.Ejbc) for type ejbc
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.WLStop) for type wlstop
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.WLRun) for type wlrun
Could not load a dependent class (javax/media/jai/PlanarImage) for type image
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.DDCreator) for type ddcreator
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckin) for type stcheckin
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckout) for type stcheckout
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel) for type stlabel
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamList) for type stlist
Could not load class (org.apache.tools.ant.taskdefs.optional.scm.AntStarTeamCheckOut) for type starteam
Could not load class (org.apache.tools.ant.taskdefs.repository.MavenRepository) for type mavenrepository
Could not load a dependent class (jdepend/xmlui/JDepend) for type jdepend
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.Ejbc) for type ejbc
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.WLStop) for type wlstop
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.WLRun) for type wlrun
Could not load a dependent class (javax/media/jai/PlanarImage) for type image
Could not load class (org.apache.tools.ant.taskdefs.optional.ejb.DDCreator) for type ddcreator
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckin) for type stcheckin
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamCheckout) for type stcheckout
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamLabel) for type stlabel
Could not load class (org.apache.tools.ant.taskdefs.optional.starteam.StarTeamList) for type stlist
Could not load class (org.apache.tools.ant.taskdefs.optional.scm.AntStarTeamCheckOut) for type starteam

Recommended Answers

All 13 Replies

note: Line numbers are matched correctly

I don't believe they are. Line 32 in this numbering doesn't call PickWord(). I don't know exactly which call you're looking at here, and I don't know which line triggered the NoSuchElementException, but that exception is thrown by a Scanner when you try to get the next() element, and there isn't one. There are two places where you call next() here, and neither of them checks to see that there's anything to get when you call them. One of those will be your culprit.
(Exceptions are documented at the Sun API, search on the name of the exception and you're likely to go directly to their docs. This will tell you what sort of things throw that Exception, which will help you figure out this sort of problem)

To figure out what's causing the problem, you'll want to look at two things. First, are you sure that you're not going off the end of the list of words when you go through your loop? How could you establish that?
If you're sure of that, what happens when you make that next call, the one that happens outside the loop? Is there an element to get when you make that call? What is that call doing for you, and if you need it there, how can you make sure it's done safely?

I have checked it against the code on my pc and the last line is 64 in both places and the main statement is on line 11 in both places. I have noticed that the exceptions show up in the same place no matter what is on the line they are always line 53 and 32. I have tried restarting my system and netbeans with no change in the output. I have also relocated statements inside certain brackets to outside the brackets and vice versa. The CountWords method in the code should limit the number of times s.next() is called in the PickWord method so I don't think there is an error with the number of times the statement is called.

Could there be an error with using the same scanner in both instances? (as in the scanner is at the end of the file when the PickWord method starts skipping words to get to the one it has randomly chosen, so there are no words to skip causing an error)

As far as I can tell the number after input is entered is a random number between 0 and 199. It should be a random double value between 1 and the total number of words in the file which in this case is 39. Any ideas?

Line 52 does not call anything that could throw that exception. Don't worry about the line numbers, though. You know where the problem is: it's at one of the calls to Scanner.next().
Now you have to trap that error.

You know it's happening in here somewhere:

for(int i = 0; i < randomInt; i++) {
            s.next();
        }
        word = s.next();

If it's getting through the for loop, then the problem is with the assignment to word. Use a print statement after the loop to figure out whether it's getting, or whether the exception is happening during the loop part.
If it's getting out of the loop, then you can focus on the word=s.next(); part, if not, then you'll want to look at what's going on in the loop.

It is not even making it to the loop the farthest it is making it is line 45 where it prints the random double value. I tried putting in print statements with various messages at the beginning of the loop at the end of the loop and after the loop with none of them showing up. I found this by removing my System.out.print(); statements until that number disappeared from the output but for some reason the value is not printing as a double.

In my first post it showed up as 54 after the exception was thrown. It has been showing up directly before that exception as any random number from 0-199.

One question I have for you - how are you even getting to that point? You're testing against the char '1', not the number 1, so how are you getting past your difficulty selection part?

I promise you that you're getting at least to the for loop. I know that because that's the first place where you can get the exception you're seeing.

I have run the file testing the input against char and against int and I am getting the same output, a random number and the same exception.

I removed all the System.out.print Statements besides the instructional statements but am still getting a number after The initial imput of 1.
I rewrote my code and I am still getting the same output:

package ProgrammingExcercises.Ch09;

/**
 *
 * @author Justin VanDeBrake
 */
import java.util.Scanner;
import java.io.File;

public class HangmanFile33 {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        
        File Easy =new File("9_33Easy.txt");
        File Medium =new File("9_33Medium.txt");
        File Hard =new File("9_33Hard.txt");
        File Extreme =new File("9_33Extreme.txt");
        
        Scanner inputEa = new Scanner(Easy);
        Scanner inputMe = new Scanner(Medium);
        Scanner inputHa = new Scanner(Hard);
        Scanner inputEx = new Scanner(Extreme);
        
        String word;
        
        System.out.println("Press 0 to exit \nor \nEnter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): ");
        int Diff = input.nextInt();
        
        if(Diff == 0)
            System.exit(0);
        else if(Diff == 1) {
            word = PickWord(inputEa, Easy);
        }
        else if(Diff == 2) {
            word = PickWord(inputMe, Medium);
        }
        else if(Diff == 3) {
            word = PickWord(inputHa, Hard);
        }
        else if(Diff == 4) {
            word = PickWord(inputEx, Extreme);
        }
        else {
            System.out.println("Invalid Difficulity. \n Please try again.");
        }
    }
    public static String PickWord(Scanner s, File f) throws Exception{
        String word;
        double randomDouble = 2.1;          //Math.random() * CountWords(f);
        int randomInt = (int)randomDouble;
        for(int i = 0; i < randomInt; i++) {
            s.next();
        }
        word = s.next();
        return word;
    }
    public static int CountWords(File f) throws Exception{
        int wordCount = 0;
        Scanner s = new Scanner(f);
        while(s.hasNext()) {
            s.next();
            wordCount++;           
        }
        return wordCount;
    }
}

and here is the output:

run:
Press 0 to exit 
or 
Enter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): 
1
138
Exception in thread "main" java.util.NoSuchElementException
        at java.util.Scanner.skip(Scanner.java:1706)
        at java.util.Scanner.skip(Scanner.java:1723)
        at ProgrammingExcercises.Ch09.HangmanFile33.PickWord(HangmanFile33.java:53)
        at ProgrammingExcercises.Ch09.HangmanFile33.main(HangmanFile33.java:32)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

""

@darklightining7

Hi I have worked on your code and solved the error. Here I give the working code. Pls revert to me if any problem exists.

Best Regards
Privin Thomas

import java.util.Scanner;
import java.io.File;

public class HangmanFile33 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);

Scanner inputEa = new Scanner(new File("9_33Easy.txt"));
Scanner inputMe = new Scanner(new File("9_33Medium.txt"));
Scanner inputHa = new Scanner(new File("9_33Hard.txt"));
Scanner inputEx = new Scanner(new File("9_33Extreme.txt"));

String word;

System.out.println("Press 0 to exit \nor \nEnter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): ");
int Diff = input.nextInt();

if(Diff == 0)
System.exit(0);
else if(Diff == 1) {
word = PickWord(inputEa);
}
else if(Diff == 2) {
word = PickWord(inputMe);
}
else if(Diff == 3) {
word = PickWord(inputHa);
}
else if(Diff == 4) {
word = PickWord(inputEx);
}
else {
System.out.println("Invalid Difficulity. \n Please try again.");
}
}
public static String PickWord(Scanner s){
//String word;
int wordcount = CountWords(s);
double randomDouble = Math.random() * wordcount;
System.out.println("Random Double:"+randomDouble);
int randomInt = (int)randomDouble;
System.out.println("Random int:"+randomInt);
/* for(int i = 0; i < (randomInt-1); i++) {
s.next();
}*/
System.out.println("Total Word Count is:"+wordcount);
return "";
}
public static int CountWords(Scanner s) {
int wordCount = 0;
String word="";
while(s.hasNext()) {
word = s.next();
wordCount++; 
}

System.out.println("Last Word of File: "+word);

return wordCount;
}
}

Can you try my code and see if it works on your system because your code is producing the same error which makes absolutely no sense.

Privin - as far as I can tell, what you've done removes the point of the exercise. PickWord() is meant to select a word at random from the text file, I don't see how your version does that.


DarkLightning - when I run your code on my machine, I get your exception on this line:

for(int i = 0; i < randomInt; i++) {
            s.next();

Now, do you see how you might get a NoSuchElementException from this sort of loop? It should jump right out at you: that exception is

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

(according to Sun's documentation). So, how could this be? Under what conditions could a Scanner produce this in response to a next() call?
(I'm asking for the simplest answer possible here)

Hi,
The s.next method scans for each token(word) in the file. But after reaching the end of file, the call of s.next method will throw an exception.

Suppose that your file has only 3 words in it, then

for( int i=0; i <4; i++)
{
s.next();
}

will throw an error when it executes s.next for the 3rd time.

Keeping this in mind try to write code. Ensure that the value of "randomInt" is less than or equal to the total number of words before executing the code

for(int i = 0; i < randomInt; i++) {
s.next();

Hope you find this post useful.

Best regards
Privin Thomas

The point of this exercise was to use regular expressions and other text manipulating and reading expressions to make a text only hangman game but since I can't get past reading the file the game has not gotten very far.

I understand that the scanner.next() expression will throw an exception when it reaches the end of the file it is reading.

The random int will always be smaller than the total number of elements in the file because it is based off the length of the file. Unless for some reason the scanner is reading the char in the file then randomInt would be larger than the length of the file most of the time.

I just found my error My file runs perfectly now it was not with the code at all. The file did not compile correctly for some reason. So all I did was go under my project folder delete the .class file then recompile the file in netbeans by clicking clean and build. Netbeans must not create a new class file when it compiles it must just edit the one it already has. That is my best guess as to why I was having an error.

If you all don't mind not closing this discussion yet as I want to finish this program first as any more errors may relate to what we have already covered.

Here is a somple run of the program:

Press 0 to exit 
or 
Enter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): 
1
WordCount is:39
RandomeDouble is: 38.562518901505435
RandomInt is: 38
Chosen Word is:skip
BUILD SUCCESSFUL (total time: 3 seconds)

And here is the code I used to generate it:

package ProgrammingExcercises.Ch09;

/**
 *
 * @author Justin VanDeBrake
 */
import java.util.Scanner;
import java.io.File;

public class HangmanFile33 {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        
        File Easy =new File("9_33Easy.txt");
        File Medium =new File("9_33Medium.txt");
        File Hard =new File("9_33Hard.txt");
        File Extreme =new File("9_33Extreme.txt");
        
        Scanner inputEa = new Scanner(Easy);
        Scanner inputMe = new Scanner(Medium);
        Scanner inputHa = new Scanner(Hard);
        Scanner inputEx = new Scanner(Extreme);
        
        String word;
        
        System.out.println("Press 0 to exit \nor \nEnter a Difficulty, Easy(1) Medium(2) Hard(3) or Extreme(4): ");
        int Diff = input.nextInt();
        
        if(Diff == 0)
            System.exit(0);
        else if(Diff == 1) {
            word = PickWord(inputEa, Easy);
        }
        else if(Diff == 2) {
            word = PickWord(inputMe, Medium);
        }
        else if(Diff == 3) {
            word = PickWord(inputHa, Hard);
        }
        else if(Diff == 4) {
            word = PickWord(inputEx, Extreme);
        }
        else {
            System.out.println("Invalid Difficulity. \n Please try again.");
        }
    }
    public static String PickWord(Scanner s, File f) throws Exception{
        String word;
        double randomDouble = Math.random() * CountWords(f);
        System.out.println("RandomeDouble is: " + randomDouble);
        int randomInt = (int)randomDouble;
        System.out.println("RandomInt is: " + randomInt);
        for(int i = 0; i < randomInt; i++) {
            s.next();
        }
        word = s.next();
        System.out.println("Chosen Word is:" + word);
        return word;
    }
    public static int CountWords(File f) throws Exception{
        int wordCount = 0;
        Scanner s = new Scanner(f);
        while(s.hasNext()) {
            s.next();
            wordCount++;           
        }
        System.out.println("WordCount is:" + wordCount);
        return wordCount;
    }
}

I did find one coding error though. In my code I allowed randomInt to equal the max number of words in my file which causes the error I have been having, but this time the error is showing lines of code that the actually pertain to it. The class file did have an error in it, my original code had been blended with the code I am presently using, which would cause that error almost every time. My original file was based of a set number of words which was considerably larger than my current file. That is where the random number was coming from right after the 1 in the output in my first post.


Thanks privinpt and jon.kaparsky for trying to solve my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.