Results 1 to 7 of 7
  1. #1
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    I'm trying to build an interpreter, and getting it to parse input correctly. I'm trying to set it up so that my Strings (stored in an ArrayList) are first split into an array called parts[]. I have it set so that it only splits into 2 Strings, which would be at parts[0 and parts[1], yes? So parts[0] contains the directive keyword mapped to the appropriate class in a HashMap. parts[1] contains the rest of the string that has the data to be further split and processed, and I assigned that to a String input. I've tried to design the loops to take into account that input may be null, as in the case of the EndStatement class. After lots of fiddling I got the code to compile, but running it now gives me this:

    Output from running the program:

    TPL Interpreter

    No valid command given
    [Ljava.lang.String;@10385c1
    Exception in thread "main" java.lang.ArrayArrayListIndexOutOfBoundsException: 1
    at StringSplit.parseCommands(StringSplit.java: 119)
    at StringSplit.main(StringSplit.java:119)

    What's the line [Ljava.lang.String;@10385c1 indicating? Thanks for helping.


    Source code:

    Java Code:
    import java.util.*;
    
    public class StringSplit
    {
    
    
    	public interface Directive //Map keywords to an interface
    	{	
    	
    		public void execute (String input);
    		
    	}
    		
    	
    	public class PrintlnStatement implements Directive
    	{ 
    
    		public void execute(String input)
    		{	//@Override
    		
    			String[] printlntokens = input.trim().split("[\"]");//treat everything after a double quote as a single token
    			for(int i=0; i<printlntokens.length; i++)
    			{
    				System.out.println(printlntokens);
    				
    			}
    		}
    	}
    	
    	
    	public class EndStatement implements Directive
    	{	//@Override
    		public void execute(String input)
    		{	
    			input=null;
    			System.out.print("TPL finished OK [" + " x lines processed]");
    			System.exit(0);
    		}
    	}
    	
    	
    	
    	private Map<String, Directive> commandHash= new HashMap<String, Directive>();
    	{
    		commandHash.put("PRINTLN", new PrintlnStatement());
    		commandHash.put("END", new EndStatement());
    		
    	}
    	
    	public void parseCommands()
    	{
    		List <String> myString= new ArrayList<String>();
    			myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
    			myString.add(new String("# xxx"));
    			myString.add(new String("STRING myString"));
    			myString.add(new String("PRINTLN HELLO WORLD"));
    			myString.add(new String("END"));
    	
    	
    		System.out.println("TPL Interpreter");
    		System.out.println();
    		
    		for (String listString: myString)//iterate across arraylist
    		{	if(listString.startsWith("#", 0))//ignore comments starting with #
    				{
    					continue;
    				}	
    			
    			String[] parts=listString.trim().split("[\\s]+",2);//split String into 2 parts to get commands
    			Directive directive= commandHash.get(parts[0]);//parts[0] should always be a command
    			String input=parts[1];
    			
    			if(directive!=null)//check if command is valid
    			{
    				
    				
    				boolean inputCheck=false;
    				
    				if (parts.length==1)//indicates there is only a command keyword string
    				{
    					if (inputCheck=false)
    					{
    						directive.execute(input);
    					}
    				}//close(parts.length==1)
    				
    				else 
    				{
    					if (parts.length>1)//indicates that there is command+input
    					{	
    						if(inputCheck=true)
    						{
    							//get information for subsequent processing
    							directive.execute(input);
    						}	
    						
    					}//close (parts.length>1)
    				}
    			}
    				else
    				{
    					System.out.println("No valid command given");
    				}				
    		}
    	}//close parseCommands()
    	
    	public static void main (String[]args)
    	{
    		StringSplit ss = new StringSplit();  
    		ss.parseCommands();  
    		
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    Quote Originally Posted by Luinithil View Post
    What's the line [Ljava.lang.String;@10385c1 indicating? Thanks for helping.
    That's how an array of Strings shows iself (arrays are sort of stupid when it comes to showing themselves ;-) It probably happened in line #24

    kind regards,

    Jos

    ps. Read about the Arrays utility class, it can show arbitrary arrays in a more readable format.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    Though I suspect the OP wants to be doing printlntokens[i].

  4. #4
    Luinithil is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    Thanks, that did the trick; also, was being stupid and caught that I missed printtokens[i]; thanks for noticing that too Tolls-- my class is working now but I'm not sure why the ArrayIndexOutOfBounds exception is still being triggered. Problem lies on line 80-- is there something about the syntax it doesn't like? I tried String input=parts[1].toString as well, but doesn't seem to be what the issue is about. Looking at the output from my recent compile-run attempt, I think now that there's a problem in how my program finds a certain class-- not all classes have parts[1] to get the String input from and it's somehow creating an exception. It's probably the EndStatement class making it throw fits as input is always null-- it's not supposed to have any. Thing is how do I modify the interface or the class itself to accept that parts[1] may not exist and may only have parts[0]?

    Output:


    TPL Interpreter

    No valid command given
    HELLO WORLD
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at StringSplit.parseCommands9StringSplit.java:80)
    at StringSplit.main(StringSplit.java 119)

    Java Code:
    	public interface Directive //Map keywords to an interface
    	{	
    	
    		public void execute (String input);
    		
    	}
    		
    	
    	public class PrintlnStatement implements Directive
    	{ /**	PrintlnStatement implements the PRINTLN keyword
    		*	The PRINTLN keyword writes output line by line to the console (standard out) but appends it to a new line.
    		*	Prints both string and numeric expressions; it can print as arguments passed or variables.
    		*	Syntax: 
    		*		PRINTLN "Insert text here"
    		*		PRINTLN someInt
    		*		PRINTLN someString
    		*/
    
    		public void execute(String input)
    		{	//@Override
    		
    			String[] printlnTokens = input.trim().split("[\"]");//treat everything after a double quote as a single token
    			for(int i=0; i<printlnTokens.length; i++)
    			{
    				System.out.println(printlnTokens[i]);
    				
    			}
    		}
    	}
    	
    	
    	public class EndStatement implements Directive
    	{	//@Override
    		public void execute(String input)
    		{	
    			//input=null;
    			System.out.print("TPL finished OK [" + " x lines processed]");
    			System.exit(0);
    		}
    	}
    
    public void parseCommands()
    	{
    		List <String> myString= new ArrayList<String>();
    			myString.add(new String("# A TPL HELLO WORLD PROGRAM"));
    			myString.add(new String("# xxx"));
    			myString.add(new String("STRING myString"));
    			//myString.add(new String("LET myString= \"HELLO WORLD\""));
    			//myString.add(new String("PRINTLN myString"));
    			myString.add(new String("PRINTLN HELLO WORLD"));
    			myString.add(new String("END"));
    	
    	
    		System.out.println("TPL Interpreter");
    		System.out.println();
    		
    		for (String listString: myString)//iterate across arraylist
    		{	if(listString.startsWith("#", 0))//ignore comments starting with #
    				{
    					continue;
    				}	
    			
    			String[] parts=listString.trim().split("[\\s]+",2);//split String into 2 parts to get commands
    			Directive directive= commandHash.get(parts[0]);//parts[0] should always be a command
    
    			String input=parts[1]; //PROBLEM IS HERE
    			
    			if(directive!=null)//check if command is valid
    			{
    				
    				
    				boolean inputCheck=false;
    				
    				if (parts.length==1)//indicates there is only a command keyword string
    				{
    					if (inputCheck=false)
    					{
    						directive.execute(input);
    					}
    				}//close(parts.length==1)
    				
    				else 
    				{
    					if (parts.length>1)//indicates that there is command+input
    					{	
    						if(inputCheck=true)
    						{
    							//get information for subsequent processing
    							directive.execute(input);
    						}	
    						
    					}//close (parts.length>1)
    				}
    			}
    				else
    				{
    					System.out.println("No valid command given");
    				}				
    		}
    	}//close parseCommands()
    Last edited by Luinithil; 01-13-2012 at 10:39 AM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    Which line is line 80?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    Quote Originally Posted by Tolls View Post
    Which line is line 80?
    You must be blind:

    Java Code:
                   }//close(parts.length==1)
    But I wonder if that line could've caused an AIOOBE ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please

    You shouldn't mock the afflicted...I might take it personally!

    Luckily I can't read either...

Similar Threads

  1. A few unusual jokes
    By tim in forum Entertainment
    Replies: 5
    Last Post: 01-06-2012, 08:52 AM
  2. DB Sanity 0.9.2
    By java software in forum Java Software
    Replies: 0
    Last Post: 12-14-2011, 09:05 AM
  3. Replies: 1
    Last Post: 12-08-2011, 06:07 PM
  4. DB Sanity 0.9.1
    By java software in forum Java Software
    Replies: 0
    Last Post: 11-23-2011, 06:17 PM
  5. DB Sanity 0.8.4
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-22-2011, 07:54 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
  •