Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayList problem

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Compiles but doesn't play right

    Hi, this code compiles, but it doesn't play right at all.
    The following is what I try to do with this simple command prompt game:
    • Have 2 cats hide in a random location, both stored in the ArrayList<String> called kittyLocation indicated in the 6 possible strings in the string[] possibilities
    • Have the user input a guess
    • Check the guess to see if it matches one of the possible hiding places
    • If it does, take away the location it was in from the ArrayList
    • When both locations are found, the game ends.

    The following is what happens
    No matter what, it returns that no cat was found, and it increases the int KittehsFound by 1 regardless of if the guess matched or not.
    Thanks in advance for your help!
    Heres the code:

    import java.util.ArrayList;
    import java.io.*;
     
    class GameHelper {
      public String getUserInput(String prompt) {
         String inputLine = null;
         System.out.print(prompt + "  ");
         try {
           BufferedReader is = new BufferedReader(
    	 new InputStreamReader(System.in));
           inputLine = is.readLine();
           if (inputLine.length() == 0 )  return null; 
         } catch (IOException e) {
           System.out.println("IOException: " + e);
         }
         return inputLine.toLowerCase();
      }
    }
     
    class LostCats {
    	private ArrayList<String> kittyLocation;
    	int KittehsFound=0;
    	public void setLocation(ArrayList<String> loc) {
    		kittyLocation=loc;
    	}
    	public String checkGuess(String userInput) {
    		String k="Neither of them are there. :(";
    		int index=kittyLocation.indexOf(userInput);
    		if (index>=0) {
    		        KittehsFound++;
    			k="You found one! :D";
    			kittyLocation.remove(index);
    			if (kittyLocation.isEmpty()) {
    				k="You found them both!! :DD";
    			}
    		}
    		System.out.println(k);
    		System.out.println("You have found "+KittehsFound+" of them.");
    		return k;
    	}
    }
    public class FindTheCats {
    	public static void main(String[] args) {
    		GameHelper g=new GameHelper();
    		System.out.println("HuskehGarrett and KaffehKitteh are missing!");
    		LostCats l1=new LostCats();
    		try {
    			Thread.sleep(1500);
    		}
    		catch (InterruptedException ie) {
    		}
    		System.out.println("Can you find them?");
    		System.out.println("-----------------------------------------------------------------");
    		ArrayList<String> location=new ArrayList<String>();
    		String[] possibilities={"Attic","Bedroom","Refrigerator","Basement","Yard","Pantry"};
    		int x=0;
    		int y=0;
    		boolean GameGoing=true;
    		int Guesses=0;
    		x=(int)(Math.random()*possibilities.length);
    		y=(int)(Math.random()*possibilities.length);
    		while(x==y) {
    			y=(int)(Math.random()*possibilities.length);
    		}
    		location.add(possibilities[x]);
    		location.add(possibilities[y]);
    		l1.setLocation(location);
    		String[] q=new String[possibilities.length];
    		String guess=null;
    		String result=null;
    		while(GameGoing==true) {
    			System.out.println();
    			for(int i=0;i<possibilities.length;i++) {
    				q[i]=possibilities[i];
    				if (i<possibilities.length-1) {
    					System.out.print(q[i]+", ");
    				} else {
    					System.out.print(q[i]);
    				}
    			}	
    			guess=g.getUserInput("?");
    			result=l1.checkGuess(guess);
    			if (result.equals("You found them both!! :DD")) {
    				GameGoing=false;
    			}
    		}
    	}
    }

    And regarding creating a game involving finding a kitten and a husky, it's for my girlfriend.
    Last edited by Renhik; July 8th, 2012 at 12:29 AM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: ArrayList problem

    Hello Renhik!
    IMO you have too much code for just a guessing game. You don't need so many methods and definitely you don't need three classes for this (if you are not doing it for practice and getting familiar with them). For example, GameHelper is unnecessary - it has only one method which can be added to FindTheCats.
    To your problem now, try adding some print statements for location and guess in the while loop and you will find out what's going on.
    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    Renhik (July 8th, 2012)

  4. #3
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: ArrayList problem

    This is not a problem with your ArrayList, but rather with your logic. The ArrayList kittyLocation is being populated with your locations exactly as you have typed them out. The entries in kittyLocation contain upper and lower case characters. So, if Attic and Bedroom are the two locations in kittyLocation and a user types in attic, it will compare to false since attic is not equal to Attic.
    Just take out the toLowerCase() function on your user input and if the user types in the location exactly as it is displayed your program will work fine.
    Last edited by ShadeDarkan; July 8th, 2012 at 05:41 AM.

  5. The Following User Says Thank You to ShadeDarkan For This Useful Post:

    Renhik (July 8th, 2012)

  6. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList problem

    Thank you both, it's resolved now.

Similar Threads

  1. ArrayList Sorting Problem
    By coke32 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 29th, 2012, 08:53 AM
  2. PhoneBook entries with ArrayList problem
    By gpelefty9 in forum Collections and Generics
    Replies: 3
    Last Post: October 30th, 2011, 03:32 AM
  3. [SOLVED] Problem copying an arraylist to an array
    By allhalf425 in forum Collections and Generics
    Replies: 4
    Last Post: September 20th, 2011, 10:25 AM
  4. Problem with ArrayList
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2010, 08:37 PM
  5. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM

Tags for this Thread