Results 1 to 3 of 3
  1. #1
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default Array index out of range: 1 HELP!

    i cant find out what im doing wrong!
    Java Code:
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Stack;
    
    
    public class Compiler 
    {	
    	static int j,i = 0;
    	static String CI; //CommandInput
    	static Scanner I = new Scanner(System.in); //demanded Input
    	static Stack M = new Stack(); //memory
    	static int Mindex = 0;
    	static int[][] O; //output (8x8 screen)
    	static String ScopeVar , ScopeOp;
    	static int ScopeVal;
    	public static void main(String[] args) {
    		CI = I.next();
    		M.push(0);
    		while(i !=CI.length())
    		{
    			CheckOps(i);
    			CheckArgs(i);
    			CheckVals(i);
    			i++;
    		}
    	}
    	
    
    	public static int CheckOps(int a)
    	{
    		int SKIP = 0;
    		int cv;
    		if(CI.charAt(i) == ' '){SKIP = 1;}
    		else if(CI.charAt(i) == '+')
    		{
    			cv = Integer.parseInt((M.get(Mindex)).toString());
    			M.set(Mindex,cv+1);
    		}else if(CI.charAt(i) == '-')
    		{
    			cv = Integer.parseInt((M.get(Mindex)).toString());
    			M.set(Mindex,cv-1);
    		}else if(CI.charAt(i) == '!')
    		{
    			System.out.printf("%d",M.pop());
    		}else if(CI.charAt(i) == '>')
    		{
    			Mindex+=1;
    			if(Mindex > M.size())
    			{
    			M.push(0);
    			}
    		}else if(CI.charAt(i) == '<')
    		{
    			Mindex-=1;
    			if(!(Mindex <1))
    			{
    			M.add(Mindex,0);
    			}
    		}
    		return(SKIP);
    	}
    	public static void CheckArgs(int a)
    	{
    		
    	}
    	public static void CheckVals(int a)
    	{
    		
    	}
    }
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
    	at java.util.Vector.get(Unknown Source)
    	at Compiler.CheckOps(Compiler.java:37)
    	at Compiler.main(Compiler.java:22)

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

    Default

    Quote Originally Posted by TheBreadCat View Post
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
    	at java.util.Vector.get(Unknown Source)
    	at Compiler.CheckOps(Compiler.java:37)
    	at Compiler.main(Compiler.java:22)
    Check line #37 of your code; you're trying to get element #1 from a Vector while it contains only one element (only index value #0 is valid). Print out all variable values you don't trust just before you try to get that element.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Check line #37 of your code; you're trying to get element #1 from a Vector while it contains only one element (only index value #0 is valid). Print out all variable values you don't trust just before you try to get that element.

    kind regards,

    Jos

    but should'nt the
    Java Code:
    if(Mindex = M.size()){M.push(0);}
    part get rid of that?

Similar Threads

  1. Replies: 2
    Last Post: 01-07-2011, 12:50 PM
  2. String index out of range: -1 Please Help
    By davetheant in forum New To Java
    Replies: 22
    Last Post: 09-11-2010, 12:32 AM
  3. Range within an Array
    By End in forum New To Java
    Replies: 6
    Last Post: 04-18-2009, 06:53 PM
  4. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 08:53 PM
  5. Replies: 3
    Last Post: 08-24-2008, 01:51 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
  •