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 15 of 15

Thread: problem using ArrayList

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post problem using ArrayList

    import java.util.*;
    import javax.swing.JOptionPane;
    public class DotComTestDrive 
           {
             public static void main(String[] args)
                  {
    	            int numOfGuesses = 0;
                        DotCom dot = new DotCom();
                        int random = (int)(Math.random() * 5);
    		    int[] locations = {random,random + 1,random + 2};
    	            dot.setLocationCells(locations);
    		    boolean isAlive = true;
     
                        while(isAlive == true)
                            {
                   			String guess = JOptionPane.showInputDialog(null,"Enter the Number");
                   			String result = dot.checkGuess(guess);
            		}
            	}
     
           }
     
             public class DotCom 
              {
                    private ArrayList<String> locationCells;
     
                   	public void setLocationCells(ArrayList<String> loc)
                 {
                   		locationCells = loc;
           	     }
     
                 public String checkGuess(String userInput)
                 {
            		String result = "miss";
    		        int index = locationCells.indexOf(userInput);
     
                            if(index >= 0)
                           {
            			locationCells.remove(index);
    			        if(locationCells.isEmpty())
                                    {
    			        	result = "kill";
             			}
    		       else result = "hit";
     
    		       }
    		return result;
               }
    }
    Last edited by omitewary; August 12th, 2014 at 01:37 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem using ArrayList

    What exactly is the problem?

    Can you fix your code tags?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: problem using ArrayList

    problem is in the line dot.setLocationCells(locations);
    ERROR : The method setLocationCells(ArrayList<String>) in the type DotCom is not applicable for arguments(int[])

    I'm beginner in java..I'm not very sure but I guess its because m passing argument of type int while calling setLocationcells but in the DotCom class I'm passing parameter of type ArrayList

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem using ArrayList

    Until you fix your code tags it's going to be pretty hard to read your code.

    But the error says it all: the method takes an ArrayList, and you're passing in an array. That's not going to work.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList


  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList

    sorry but I'm unable to get it what exactly you are saying about fixing code tags.I tried to rearrange my codes but it is again setting into same format.Sorry for the inconvinience.I ve sent attachment.Guess it ll be easier to read.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    GregBrannon (August 12th, 2014), omitewary (August 12th, 2014)

  9. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList

    thnx a lot..Do you have any solution to this?now that the program is readable,Please me with any changes in the program that would work.

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem using ArrayList

    Well, like I said, the method takes an ArrayList, and you're passing in an array. Which do you want to do: pass in an array, or accept an ArrayList? It's up to you which one is correct.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList

    I want to pass an ArrayList only but for that again I ve to change my Array of type int which is accepting random number.How do I fix that?I tried putting random no. first in ArrayList then passing it on setLocationCells but then also its not working

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: problem using ArrayList

    This is the long way around the barn to generate a "miss", "kill" or a "hit" result. Does it have to be this complicated? Why not generate a random number, 0, 1, or 2, and select "miss", "kill" or "hit" from a String array, { "miss", "kill", "hit" }.

    If you must stick with this complicated contraption, then use an ArrayList<Integer> in which to place the random integers, OR stick with an int[] array throughout.

  13. #12
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList

    I did with String array only..but it is having one bug.suppose if its getting hit with any number say '2', n again when I press 2 2nd and 3rd tym it gets hit.Then I found out the prob is my first guess which is hit is getting stored in the array,so whenever m pressing the no. again its is showing hit.so to remove the index which is getting hit I chose ArrayList

  14. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: problem using ArrayList

    Solved then?

  15. #14
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: problem using ArrayList

    No

  16. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: problem using ArrayList

    Then post an updated MCVE and ask a specific question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. ArrayList problem.
    By ben1 in forum Collections and Generics
    Replies: 1
    Last Post: May 29th, 2014, 10:20 AM
  2. Arraylist get problem
    By soniabobit in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2013, 06:40 PM
  3. ArrayList problem
    By Renhik in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 11:48 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