Results 1 to 4 of 4
  1. #1
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Default Traversing a list and comparing

    Hi all,

    I'm working on a java Hanjie solver and this is part of the code. Everything else seems okay but I always get an error when I run it.

    Eclipse (i'm using Galileo version) points the exception to be at:

    Java Code:
    char a = combo1.get(i).charAt(i);
    The section of the code is:
    Java Code:
    for ( i = 0; i < line.size(); i++)
    		{
    			boolean same = false;
    			char a = combo1.get(0).charAt(i);
    			int postn = 0;
    			for (int x = 1; x < combo1.size(); x++)
    			{
    				if (combo1.get(x).charAt(i) == a)
    				{
    					same = true;
    					postn = x;
    				}
    			}
    combo1
    ...refers to a List<String> of all possible combinations of black and white assignments (where '1' represents black and white '0').

    Essentially, this code snippet supposedly picks the first item (a string of 1s and 0s), gets the character at a particular position and compares it with the characters in the same position in the rest of the items in the list. Apparently, if they overlap, then this assignment should be applied to the line.

    I'm assuming the error (which Eclipse gives as java.lang.IndexOutOfBoundsException) must be to do with something other than this particular line but I'm just curious if there IS anything wrong with this part itself.

    There's a whole lot of code surrounding this one and I don't want to paste the whole thing lest it confuses myself and anyone else more.

    It looks like I will have to anyway if there's nothing with this code in particular...

    And another question; when removing an item from a list, does everything after/before it shift up/down?

    Thanks in advance

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    We'll need to see the rest of the code. The code seems to be assuming that combo1.get(0) has at least as many characters as line (whatever it is) has elements. And that assumption seems to be faulty.

    -Gary-

  3. #3
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Unhappy Here's the rest of the code...

    Quote Originally Posted by gcalvin View Post
    We'll need to see the rest of the code. The code seems to be assuming that combo1.get(0) has at least as many characters as line (whatever it is) has elements. And that assumption seems to be faulty.

    -Gary-
    Thanks for the reply Gary, here's the rest, or rather, all of the code (I'm not sur if I'm allowed to do that?) References to other classes not in the code are from another package:

    Java Code:
    public class MyHanjieSolver3 implements Solver {
    	
    	
    	boolean row;
    	boolean column;
    	int last;
    	
    	public MyHanjieSolver3()
    	{
    		this.row = false;
    		this.column = true;
    		this.last = -1;
    	}
    //********************************************************************************************************************************************************************
    	
    	@Override
    	public void checkConsistency(Grid hanjie) throws HanjieException {
    		// TODO Auto-generated method stub
    		
    		int i, j;
    		String line = null;
    		Value v;
    		if (column)
    		{
    			for (i = 0; i < hanjie.getHeight(); i++)
    			{
    				for (j = 0; j < hanjie.getHeight(); j++)
    				{
    					
    					v = hanjie.getColumn(i).getCellAt(j).getValue();
    					if (v == Value.BLACK)
    						line = line + "1";
    					else if (v == Value.WHITE)
    						line = line + "0";
    					else if (v == Value.UNKNOWN) 
    						line = line + "2";
    				}
    				
    				Pattern pattern = hanjie.getColumn(i).getConstraintsAsRegularExpression();
    				boolean matched = pattern.matcher(line).matches();
    				
    				if (!matched)
    					throw new HanjieException();
    			}
    		}
    		
    		else
    		{
    			for (i = 0; i < hanjie.getWidth(); i++)
    			{
    				for (j = 0; j < hanjie.getWidth(); j++)
    				{
    					
    					v = hanjie.getRow(i).getCellAt(j).getValue();
    					if (v == Value.BLACK)
    						line = line + '1';
    					else if (v == Value.WHITE)
    						line = line + '0';
    					else if (v == Value.UNKNOWN)
    						line = line + '2';
    				}
    				
    				Pattern pattern = hanjie.getRow(i).getConstraintsAsRegularExpression();
    				boolean matched = pattern.matcher(line).matches();
    				
    				if (!matched)
    					throw new HanjieException();
    			}
    		}
    		
    	}
    
    //********************************************************************************************************************************************************************
    	
    	@Override
    	//Checks if hanjie puzzle is complete...
    	public boolean isComplete(Grid hanjie) {
    		// TODO Auto-generated method stub
    		//return false;
    		
    		for(int i = 0; i < hanjie.getWidth(); i++)
    		{
    			for(int j = 0; j < hanjie.getHeight(); j++)
    			{
    				if(hanjie.getColumn(i).getCellAt(j).getValue() == Value.UNKNOWN)
    					return false;
    			}
    		}
    		
    		for(int i = 0; i < hanjie.getHeight(); i++)
    		{
    			for(int j = 0; j < hanjie.getWidth(); j++)
    			{
    				if(hanjie.getRow(i).getCellAt(j).getValue() == Value.UNKNOWN)
    					return false;
    			}
    		}
    		
    		return true;
    	}
    
    //********************************************************************************************************************************************************************
    	
    	@Override
    	public Line solveNext(Grid hanjie) {
    		// TODO Auto-generated method stub
    		
    		this.last += 1;
    		Line line = null;
    		Line tempLine = null;
    		
    		if(this.row == true)
    			line = hanjie.getRow(last);
    		if(this.column == true)
    			line = hanjie.getColumn(last);
    		
    		//********************************************************************
    		
    		if (this.column)
    		{
    			line = hanjie.getColumn(this.last);
    			tempLine = drawLine(hanjie, this.column, line);
    			line = tempLine;
    			
    		}
    		
    		else
    		{
    			line = hanjie.getRow(this.last);
    			tempLine = drawLine(hanjie, this.column, line);
    			line = tempLine;
    		}
    		
    		//********************************************************************
    		
    		if(this.row == true && this.last == hanjie.getHeight() - 1)
    		{
    			this.row = false;
    			this.column = true;
    			this.last = -1;
    		}
    		if(this.column == true && this.last == hanjie.getWidth() - 1)
    		{
    			this.row = true;
    			this.column = false;
    			this.last = -1;
    		}
    		
    		return line;
    }
    	
    //********************************************************************************************************************************************************************
    
    	public static Line drawLine (Grid hanjie, boolean column, Line line)
    	{
    		
    		int i;
    		//list one of all combinations...
    		List<String> combo1 = new ArrayList<String>();
    		
    		
    		//creates list of possible all combinations using combos() method...
    		if (column)//if line is a column, create combinations to fit vertically...
    			combos1(hanjie, hanjie.getHeight(), combo1);
    		
    		else //if line is a row, create combinations to fit horizontally...
    			combos1(hanjie, hanjie.getWidth(), combo1);
    		
    		//********************************************************************
    		//going through combo1 and removing combinations inconsistent with assignments already made on the cells....
    		
    		if (column)
    		{
    			combos2(hanjie, hanjie.getHeight(), combo1, line);
    		}
    		
    		else
    		{
    			combos2(hanjie, hanjie.getWidth(), combo1, line);
    		}
    		
    		//********************************************************************
    		
    		//combo1 has been updated...
    		//now eliminating possible combinations that are inconsistent with the current line's constraints...
    		
    		String match = null;
    		for (i = 0; i < line.size(); i++)
    		{
    			if (line.getCellAt(i).getValue() == Value.BLACK)
    				match = match + '1';
    			else if (line.getCellAt(i).getValue() == Value.WHITE)
    				match = match + '0';
    			else
    				match = match + '2';
    		}
    		
    		Pattern pattern = line.getConstraintsAsRegularExpression();
    			
    			
    		for (i = 0; i < combo1.size(); i++)//goes through list
    		{
    			boolean matched = pattern.matcher(match).matches();
    				
    			if (!matched)
    				combo1.remove(i);//removes combination from list...
    		}
    		
    		
    		//********************************************************************
    		
    		//combo1 has been updated...
    		//now finding overlapping assignments in the possible combinations leftover from the eliminations...
    		
    		for ( i = 0; i < line.size(); i++)
    		{
    			boolean same = false;
    			char a = combo1.get(0).charAt(i);
    			int postn = 0;
    			for (int x = 1; x < combo1.size(); x++)
    			{
    				if (combo1.get(x).charAt(i) == a)
    				{
    					same = true;
    					postn = x;
    				}
    			}
    			
    			if (same == true)
    			{
    				if (a == '1')
    					line.getCellAt(postn).setValue(Value.BLACK);
    				else if (a == '0')
    					line.getCellAt(postn).setValue(Value.WHITE);
    				
    			}
    			else
    				line.getCellAt(postn).setValue(Value.UNKNOWN);
    					
    		}
    		
    		//********************************************************************
    		
    	return line;
    }
    	
    	//********************************************************************************************************************************************************************
    	//method to create a list of all possible combinations...
    	//method asks for the current hanjie puzzle, line's size and current list to update...
    	public static void combos1 (Grid hanjie, int j, List<String> combo1)
    	{
    		String str;
    		for (int i = 0; i < j; i++)
    		{
    			str = Integer.toBinaryString(i);
    			str = zeroFiller(Integer.toBinaryString(i),j,str.length()); //converts to binary and fills in the leading zeros...
    			combo1.add(str);
    		}
    		
    	}
    	
    	//********************************************************************************************************************************************************************	
    	//method to eliminate/remove possible combinations that are inconsistent with assignments already made on current line...
    	//method asks for the current hanjie puzzle, line's size, current list to update and current line...
    	public static void combos2 (Grid hanjie, int j, List<String> combo1, Line line)
    	{
    		
    		for (int i = 0; i < j; i++)//go through entire line...
    		{
    			for (int x = 0; x < combo1.size(); x++)//go through entire list...
    			{
    				if (((combo1.get(x).charAt(i) == '1')&& (line.getCellAt(i).getValue() == Value.BLACK)) || ((combo1.get(x).charAt(i) == '0')&& (line.getCellAt(i).getValue() == Value.WHITE)))
    				{
    					combo1.remove(x);
    				}
    			}
    		}
    	}
    	
    //********************************************************************************************************************************************************************
    	
    	//method to add zeros in front of combinations to get correct length..
    	//method asks for string to fill, length/width of current column/row and length of string to fill...
    	public static String zeroFiller (String tofill, int hanjiex, int i)
    	{
    		if ( i == hanjiex )
    		return tofill;
    		else
    		{
    			int j = hanjiex - i;
    			for (int k=0; k<j; k++)
    			{
    				tofill = "0" + tofill;
    			}
    			return tofill;
    		}
    	}
    }

  4. #4
    chico9 is offline Member
    Join Date
    Apr 2010
    Location
    At my desk
    Posts
    9
    Rep Power
    0

    Thumbs up Nevermind...

    Ignore all of that, I've got new problems now ):
    My hanjie solver fills in the cells but it's all wrong! But I'll get to it soon enough (I think).

    Thanks Gary for responding.

Similar Threads

  1. [UnSolved] Traversing Trees problem
    By RllyNew2Java in forum New To Java
    Replies: 8
    Last Post: 12-24-2009, 07:59 PM
  2. Comparing List of maps
    By thorne_ in forum New To Java
    Replies: 1
    Last Post: 06-10-2009, 02:30 AM
  3. Traversing a JTabbedPane
    By Inks in forum AWT / Swing
    Replies: 12
    Last Post: 03-11-2009, 05:15 AM
  4. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  5. Traversing a directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:33 AM

Posting Permissions

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