Results 1 to 13 of 13
  1. #1
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default ArrayList<String>

    Hi,
    Back again for a different attempt.

    The code below is my attempt to add the Strings called "textGuess" and "feedBack" to "textHistory" and "feedBackHistory" ArrayLists through a while loop (MAX 12 loops). I have added extra System.out.println("") code lines to verify that the Strings are being passed into the Methods and then added to the ArrayLists and a further line of code to print the size of the ArrayList through each pass of the While loop.

    All lines of code noted "CONFIRM STRING PASS" and "CONFIRM ADD" perform as expected but the line of code "CHECK ARRAYLIST SIZE" always prints "0" so the following WHILE loop "while (historyLoop < size)" does not fire as all. It would appear that these strings are not being added permanently but I have no clue whatsoever.

    I would be very grateful for some assistance.

    Example of the strings being passed,
    "textGuess" = "Black, Blue, Green, Red"
    "feedBack" = "B, B, X, X"


    Regards

    Pete

    Java Code:
    public class GameHistory
    {
    	// STATIC FINAL Variables
    	private static final int CODE_SIZE = 4;
    
    	private int textHistoryLoop = 0, feedBackHistoryLoop = 0;
    
    	// Create Scanner Class Object "scan"
    	private Scanner scan;
    
    	// Class ArrayList
    	private ArrayList<String> textHistory = new ArrayList<String>();
    	private ArrayList<String> feedBackHistory = new ArrayList<String>();
    
    	// Method addTextGuessHistory()
    	// Add User Text Guess
    	// Pass in "textGuess" String
    	public void addTextGuessHistory(String textGuess)
    	{
    		// CONFIRM STRING PASS
    		System.out.println("@ PASS " + textGuess);
    
    		// Add "textGuess" String to "textHistory" ArrayList
    		textHistory.add(textGuess);
    
    		// CONFIRM ADD
    		System.out.println("@ ADD " + textHistory.get(textHistoryLoop));
    
    		// Add 1 to "textHistoryLoop"
    		textHistoryLoop++;
    
    	} // End of addTextGuessHistory() Method
    
    	// Method addFeedBackHistory()
    	// Add User FeedBack
    	// Pass in "feedBack" Array
    	public void addFeedBackHistory(String feedBack)
    	{
    		// CONFIRM STRING PASS
    		System.out.println("@ PASS " + feedBack);
    
    		// Add "feedBack" String to "feedBackHistory" ArrayList
    		feedBackHistory.add(feedBack);
    
    		// CONFIRM ADD
    		System.out.println("@ ADD " + feedBackHistory.get(feedBackHistoryLoop));
    
    		// Add 1 to "feedBackHistoryLoop"
    		feedBackHistoryLoop++;
    
    	} // End of addFeedBackHistory() Method
    
    	// Method grabHistory()
    	// Get Strings from numberHistory and feedBackHistory ArrayLists @ "historyLoop" indexes
    	// Pass in "gameLoop" Integer
    	public void grabHistory(int gameLoop)
    	{
    		// Method Variables
    		int historyLoop = 0;
    
    		int size = textHistory.size();
    
    		// CHECK ARRAYLIST SIZE
    		System.out.println(size);
    
    		// Arrays
    		String[] guessNumber = {"One", "Two", "Three", "Four", "Five", "Six",
    		"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"};
    
    		// Loop WHILE
    		// Loop While "historyLoop" is Less Than "numberHistory" Array Size
    		while (historyLoop < size)
    		{
    			// Display User History
    			System.out.println("\n**************************************************");
    			System.out.println("** Guess " + guessNumber[gameLoop]);
    			System.out.println("**************************************************");
    			System.out.println("** Colours = " + textHistory.get(historyLoop));
    			System.out.println("** FeedBack = " + feedBackHistory.get(historyLoop));
    			System.out.println("**************************************************");
    
    			// Add 1 to "historyLoop"
    			historyLoop++;
    
    		} // End of While Loop
    	} // End of grabHistory() Method
    } // End of GameHistory Class
    Last edited by PeteW44; 03-22-2012 at 03:00 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: ArrayList<String>

    Show us more code, for example with a main method.
    Your method look fine....
    Java Code:
        public static void main(String[] args) {
    		GameHistory his = new GameHistory();
    		his.addTextGuessHistory("Hello");
    		his.addFeedBackHistory("World");
    		his.grabHistory(1);
    	}
    I get the output
    @ PASS Hello
    @ ADD Hello
    @ PASS World
    @ ADD World
    1
    and the while loop fired. So your problem is somewhere else

  3. #3
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    Thanks for your reply and your time taken to investigate this for me I really appreciate this. I understand what you have done, the only difference I can see from what you have done is that I am adding the same String variable with different content and not a unique String literal does this make any difference? I have alot of code you may not want to look at all that.


    Regards

    Pete

  4. #4
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    I have added System.out.println(textHistory.size()); and System.out.println(feedBackHistory.size()); Just after the ADD to ArrayList commands and also in the GRAB HISTORY Method prior to the WHILE LOOP instead of the int size local variable. I have changed the while loop condition to (historyLoop < gameLoop)

    In both add Methods the size() command prints "1" and then in the GET DISPLAY Method this is back to "0", after the second loop which displays the same there is a crash with the following,

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)

    Does this suggest that new ArrayLists are being created and the old ones overwritten with each game loop.? I am brain dead looking at this.

    Regards

    Pete

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: ArrayList<String>

    IndexOutOfBoundsException means that in your Array or ArrayList/Collection (any type of array) there was an attempt to access an element at an index that didn't exist or was invalid.

    In this case the Size was 0 and the index out of the range was 0. There are two things you should know straight away from that error message:
    a) that your Array was EMPTY (size: 0)
    b) index 0 is out of range (of course, because there are NO indexes since the Array is empty).

    There is a very easy way to get around that; just check if your ArrayList is empty first, if its empty don't loop through it/ dont try to access any element from it.

    The method you need is ArrayList.isEmpty().

  6. #6
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    Hi,
    Thanks 4 your input, the ArrayList should not be empty, as every pass through the loop adds 1 element to each of the 2 ArrayLists. The call statements in the add methods print to screen as below then the grab method says both are empty? do you know why size() would go from 0, to 1 and back to 0 in a single loop through a series of methods, does this suggests that the String is added and then its removed?


    Regards

    Pete


    IN 1 LOOP
    Method addTextGuessHistory
    1 (Size() of textHistory ArrayList) System.out.println(textHistory.size());
    @ ADD Black, Blue, Green, Red (Get textHistory(textHistoryLoop)) System.out.println("@ ADD " + textHistory.get(textHistoryLoop));

    Method addFeedBackHistory
    1 (Size() of textHistory ArrayList) System.out.println(feedBackHistory.size());
    @ ADD W, W, X, X (Get feedBackHistory(feedBackHistoryLoop)) System.out.println("@ ADD " + feedBackHistory.get(feedBackHistoryLoop));

    Method grabHistory
    0 (Size() of textHistory ArrayList) System.out.println(textHistory.size());
    0 (Size() of textHistory ArrayList) System.out.println(feedBackHistory.size());

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: ArrayList<String>

    The JVM doesn't lie; if it says the array size is 0 it means its 0.

    There must be something in your code removing it or clearing the arraylist.

    Either that or you're using the wrong variable.

    Please post the actual code - its probably just a silly mistake.

  8. #8
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    Hi Ozzy,
    Thanks again, here U go... The whole shebang is really massive...

    Kind Regards

    Pete

    Java Code:
    CUT From Game Class
    // Send Message to Class "GameHistory", Method "displayHistory()"
    aNewGameHistory.grabHistory(gameLoop);
    
    public class GameHistory
    {
    	// STATIC FINAL Field Variables
    	private static final int CODE_SIZE = 4;
    
    	// Field ArrayList
    	private ArrayList <String> textHistory = new ArrayList <String>();
    	private ArrayList <String> feedBackHistory = new ArrayList <String>();
    
    	// Create Scanner Class Object "scan"
    	private Scanner scan;
    
    
    	// Method addTextGuessHistory()
    	// Add User Text Guess
    	// Pass in "textGuess" String
    	public void addTextGuessHistory(String textGuess, int gameLoop)
    	{
    		// Add "textGuess" String to "textHistory" ArrayList
    		textHistory.add(gameLoop, textGuess);
    
    		// CONFIRM ARRAYLIST SIZE
    		System.out.println(textHistory.size());
    
    		// CONFIRM STRING ADD
    		System.out.println(textHistory.get(gameLoop));
    
    	} // End of addTextGuessHistory() Method
    
    
    	// Method addFeedBackHistory()
    	// Add User FeedBack
    	// Pass in "feedBack" Array
    	public void addFeedBackHistory(String feedBack, int gameLoop)
    	{
    		// Add "feedBack" String to "feedBackHistory" ArrayList
    		feedBackHistory.add(gameLoop, feedBack);
    
    		// CONFIRM ARRAYLIST SIZE
    		System.out.println(feedBackHistory.size());
    
    		// CONFIRM STRING ADD
    		System.out.println(feedBackHistory.get(gameLoop));
    
    	} // End of addFeedBackHistory() Method
    
    
    	// Method grabHistory()
    	// Get Strings from numberHistory and feedBackHistory ArrayLists
    	// @ "historyLoop" indexes
    	// Pass in "gameLoop" Integer
    	public void grabHistory(int gameLoop)
    	{
    		// Local Variables
    		int historyLoop = 0;
    
    		// Arrays
    		String[] guessNumber = {"One", "Two", "Three", "Four", "Five", "Six",
    		"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"};
    
    		// CONFIRM ARRAYLIST SIZE
    		System.out.println(textHistory.size());
    
    		// CONFIRM ARRAYLIST SIZE
    		System.out.println(feedBackHistory.size());
    
    		// Loop WHILE
    		// Loop While "historyLoop" is Less Than "numberHistory" Array Size
    		while (gameLoop > historyLoop)
    		{
    			// Display User History
    			System.out.println("\n**************************************************");
    			System.out.println("** Guess " + guessNumber[gameLoop]);
    			System.out.println("**************************************************");
    			System.out.println("** Colours = " + textHistory.get(historyLoop));
    			System.out.println("** FeedBack = " + feedBackHistory.get(historyLoop));
    			System.out.println("**************************************************");
    
    			// Add 1 to "historyLoop"
    			historyLoop++;
    
    		} // End of While Loop
    	} // End of grabHistory() Method
    } // End of GameHistory Class
    
    CUT From OutPutDisplay Class
    // Run sendGuess2History() Method
    sendGuess2History(gameLoop);
    
    // SEND GUESS 2 HISTORY
    // Method sendGuess2History()
    // Send FeedBack 2 History
    private void sendGuess2History(int gameLoop)
    {
    	// Method Variables
    	String textGuessCode;
    
    	// Build "textGuessCode" String to pass to GameHistory
    	textGuessCode = textColourCode[0] + ", " + textColourCode[1] + ", " +
    	textColourCode[2] + ", " + textColourCode[3];
    
    	// Create a New Instance of "GameHistory" Class
    	GameHistory aNewGameHistory = new GameHistory();
    
    	// Send Message to Class "GameHistory", Method "addTextGuessHistory()"
    	aNewGameHistory.addTextGuessHistory(textGuessCode, gameLoop);
    
    } // End of sendGuess2History() Method
    
    CUT From FeedBack Class
    // Run sendFeedBack2History() Method
    sendFeedBack2History(gameLoop);
    
    // SEND FEEDBACK 2 HISTORY
    // Method sendFeedBack2History()
    // Send FeedBack 2 History
    private void sendFeedBack2History(int gameLoop)
    {
    	// Method Variables
    	String feedBack;
    
    	// Set "feedBack" String
    	feedBack = userFeedBackCode[0] + ", " + userFeedBackCode[1] + ", " +
    	userFeedBackCode[2] + ", " + userFeedBackCode[3];
    
    	// Create a New Instance of "GameHistory" Class
    	GameHistory aNewGameHistory2 = new GameHistory();
    
    	// Send Message to Class "GameHistory", Method "addFeedBackHistory()"
    	aNewGameHistory2.addFeedBackHistory(feedBack, gameLoop);
    
    } // End of sendFeedBack2History() Method
    Last edited by PeteW44; 03-23-2012 at 10:33 PM.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: ArrayList<String>

    I did mean the actual code for the pseudo-code of the methods to do with the ArrayList that you posted before.

    Anyhow, this doesn't seem right:
    1. You're using the ArrayList.add(int index, Object element) method to add the elements
    where the value of 'index' is your parameter 'gameLoop'
    2. in your main method (from your other post) you are starting the program with gameLoop set to '1'
    3. gameLoop is never changed so you're over-writing the element at the same index '1'

  10. #10
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    Hi Ozzy,
    Cheers I have been extra confused with your request for code this program is 7 Classes on about 36 a4s I can host the .java files on mediafire, 4shared or let U hav a look with Team Viewer if you prefer. Included Snippets from Game Class which is the main WHILE loop. This loop only lets you have 12 guesses and worked fine before I added GameHistroy.

    Cheers

    Pete

    PS is not pseudo-code is cut straight from JCreator?

    MasterMind Java files

    Java Code:
    Game Class
    // Field Variables
    private int gameLoop = 0, guessLoop = 0, userGuess = 0;
    
    Last line of code before another guess takes place
    // Add 1 to "gameLoop"
    gameLoop++;
    Last edited by PeteW44; 03-24-2012 at 02:41 PM.

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: ArrayList<String>

    You just need to show all the bits of code where things are added or removed from the ArrayList so you can see where the ArrayList is being emptied.

    I can see your methods for adding to the ArrayList, but where are the methods that remove things from the ArrayList and how are they called?

    You just need to post code that is relative to the problem.

    Or you could create an SSCCE which is a simplified version of your program which displays the problem you are trying to solve.
    Short, Self Contained, Correct Example

  12. #12
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    Hi Ozzy,
    Cheers, I have no method to empty the ArrayLists the only code that relates to it is above. I will try and remove all code that I can and still make it work and see if it highlights anything but I am not sure I can do this.

    Regards

    Pete

    PS I think its because I have created different GameHistory Class Objects in both the OutPutDisplay and FeedBack Classes, How can I use the same object over different classes?
    Last edited by PeteW44; 03-24-2012 at 07:31 PM.

  13. #13
    PeteW44 is offline Member
    Join Date
    Mar 2012
    Location
    Lisburn Northern Ireland
    Posts
    15
    Rep Power
    0

    Default Re: ArrayList<String>

    I am a Silly idiot, I have created 2 get Methods, one in both OutPutDisplay and FeedBack Classes these return a String. I have used the same object to send these two Strings to GameHistory Class.

    Rearranged my Game Class to include a get method which gets the strings and passes them to GameHistory Class.

    Consider this issue resolved....

    Thanks and a BIG TY to Ozzy for your assistance....Sorry 4 wasting your time, I downloaded a trial of JCreator Pro and figured out how to debug! this showed me the objects being created the first object did as was expected the second one wiped the first out and then the first one wiped the second and so on thats why it did not work.

    Cheers and Regards

    Pete

    PS yippee

Similar Threads

  1. String to ArrayList error
    By anoorally in forum New To Java
    Replies: 3
    Last Post: 03-04-2012, 06:47 PM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. How to match arrayList with a String?
    By Lund01 in forum Advanced Java
    Replies: 2
    Last Post: 10-14-2010, 02:07 PM
  5. Putting a string into ArrayList<String>
    By k4ff1n34dd1c7 in forum New To Java
    Replies: 5
    Last Post: 03-23-2009, 05:10 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
  •