Results 1 to 7 of 7
- 01-13-2012, 09:18 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
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(); } }
- 01-13-2012, 10:07 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-13-2012, 10:17 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please
Though I suspect the OP wants to be doing printlntokens[i].
- 01-13-2012, 10:29 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
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.
- 01-13-2012, 12:34 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please
Which line is line 80?
- 01-13-2012, 12:46 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Unusual ArrayListIndexOutOfBoundsException message?? Sanity check needed please
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-13-2012, 12:59 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
A few unusual jokes
By tim in forum EntertainmentReplies: 5Last Post: 01-06-2012, 08:52 AM -
DB Sanity 0.9.2
By java software in forum Java SoftwareReplies: 0Last Post: 12-14-2011, 09:05 AM -
Proposed use of Java to enhance asp.net/mssql architecture -- sanity check please
By MyTimeFinder in forum New To JavaReplies: 1Last Post: 12-08-2011, 06:07 PM -
DB Sanity 0.9.1
By java software in forum Java SoftwareReplies: 0Last Post: 11-23-2011, 06:17 PM -
DB Sanity 0.8.4
By java software in forum Java SoftwareReplies: 0Last Post: 10-22-2011, 07:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks