Results 1 to 2 of 2
Thread: Exception when program is run.
- 09-14-2009, 12:46 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
Exception when program is run.
I get an IndexNotFoundException on line 77. The one that says "int currentScrambledWordLength = (eachScrambledWord.get(atScrambledWord)).length(); "
How do I fix this?:confused:
Java Code:/*-Author: Matthew Vaughan-*/ import java.util.*; import java.io.*; public class Unscramble { public static void main(String args[]){ Scanner myScanner = new Scanner(System.in); Scanner keyboard = new Scanner(System.in); //input from the keyboard String words; //the words that are to be unscrambled ArrayList<String> eachScrambledWord = new ArrayList<String>(); //Will contain scrambled words ArrayList<String> eachUnscrambledWord = new ArrayList<String>(); //Will contain unscrambled words read from text file int howManyWords; //how many words the user wants unscrambled System.out.println("How many words do you want unscrambled? "); howManyWords=myScanner.nextInt(); int spaces[] = new int[howManyWords-2]; System.out.print("Enter the words you want unscrambled separated by a space: "); words = keyboard.nextLine(); //all the words the user wants unscrambled for(int i = 0; i<howManyWords-2; i++){ //this for loop will find all the positions of the spaces so it can separate //the string in to individual words and store them in the array list "eachScrambledWord" if(i==0){ spaces[i] = words.indexOf(" "); }else{ spaces[i] = words.indexOf(" ", spaces[i-1] + 1); } } for(int ii=0; ii < howManyWords-2; ii++){ //adds words to the array list if(ii==0){ eachScrambledWord.add(words.substring(0, spaces[0])); }else{ eachScrambledWord.add(words.substring(spaces[ii -1] + 1, spaces[ii])); } } String currentLine; try{ BufferedReader fromFile = new BufferedReader(new FileReader("wordlist.txt")); //object that reads the text file if (!fromFile.ready()){ //makes sure reader is ready throw new IOException(); } while ((currentLine = fromFile.readLine()) != null){ //makes sure that the reader hasn't reached the end of the file eachUnscrambledWord.add(currentLine); } }catch(Exception eee){ System.out.println("IO Error\nFile may not exist in the directory of this program\nMake sure you enter the full path to the file"); } /*Hooray for 2D Arrays!*/ int[][] wordsSameLength = new int[howManyWords][eachUnscrambledWord.size()]; /*Hooray for nested for loops!*/ for(int atLine=0;atLine<eachUnscrambledWord.size();atLine++){ for(int atScrambledWord = 0;atScrambledWord<howManyWords; atScrambledWord++) { int currentScrambledWordLength = (eachScrambledWord.get(atScrambledWord)).length(); int currentUnscrambledWordLength = (eachUnscrambledWord.get(atLine)).length(); if(currentScrambledWordLength==currentUnscrambledWordLength){ wordsSameLength[atScrambledWord][atLine] = atLine; } } } } }
-
answered in cross-post at forums.sun.com.
Similar Threads
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
SQL Exception from Java program
By jyotigupta6 in forum New To JavaReplies: 1Last Post: 10-28-2008, 06:27 PM -
SQL Exception from java program
By jyotigupta6 in forum New To JavaReplies: 3Last Post: 10-27-2008, 06:35 PM -
hai friends i got the Exception in the following java program , how do i resolve it ?
By burepallisri in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 05:56 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks