Results 1 to 2 of 2
  1. #1
    m0v0n is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default 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;
    							}
    						}
    					}
    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 08:42 PM
  2. SQL Exception from Java program
    By jyotigupta6 in forum New To Java
    Replies: 1
    Last Post: 10-28-2008, 07:27 PM
  3. SQL Exception from java program
    By jyotigupta6 in forum New To Java
    Replies: 3
    Last Post: 10-27-2008, 07:35 PM
  4. Replies: 3
    Last Post: 04-07-2008, 06:56 PM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

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