There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell drive driver drivers email error ethernet excel explorer firefox firefox 3 freeze game hard drive internet internet explorer itunes laptop malware monitor network networking outlook outlook 2003 outlook express partition password printer problem problems ram router slow sound sprtcmd.exe trojan usb virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Remove element from ArrayList in tester class


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
virkjay's Avatar
Junior Member with 10 posts.
 
Join Date: Nov 2007
Experience: Beginner
10-Nov-2007, 10:35 AM #1
Remove element from ArrayList in tester class
Hi guys, I am trying to make vidoe poker game, so I have done Card class
public class Card
{
public Card()
{

}

public Card(String type, int no)
{
typeOfCard = type;
number = no;
}

public String getType()
{
return typeOfCard;
}

public int getNumber()
{
return number;
}

private String typeOfCard;
private int number;
}

********Then i have done code for the PlayPoker Class**********

import java.util.*;
public class PlayPoker
{
public PlayPoker()
{
deck = new ArrayList<Card>();
allotedCards = new ArrayList<Card>();
}

public void addCards(Card a)
{
deck.add(a);
}
public void addCards(String type, int no)
{
deck.add(new Card(type, no));
}

public void showDeck()
{
for(Card c : deck)
{
System.out.println(c.getType() + c.getNumber() + "" + "");
}
}

public void shuffle()
{
Collections.shuffle(deck);
}

public void cardsOfPlayer()
{
for(int i=0; i<5; i++)
{
allotedCards.add(deck.get(i));
}
}

public void showAllotedCards()
{
for(Card c : allotedCards)
{
System.out.print(c.getType() + c.getNumber());
}
}

public void cardsInDeck(int noOfCards)
{
for(int i=0; i<noOfCards; i++)
{
int index =0;
deck.remove(index);
}
}

public void swap(String a, int b) // Problem with method in tester class
{
for (Card c : allotedCards)
{
if((a.equals(c.getType()) && (b == c.getNumber())))
{
deck.add(c);
allotedCards.remove(c);
}
}
}

public void cardsToSwap(Card a) // Problem with this method in Tester class
{
for(Card c : allotedCards)
{
if(a.equals(c))
deck.add(c);
allotedCards.remove(c);
}
}
private ArrayList<Card> deck;
private ArrayList<Card> allotedCards;
}

**********This PlayPoker class is compiling perfectly but when i invoke last two methods in PlayPokerTester so i can remove element(cards) from
allotedCards(ArrayList) then it is giving errors or don't work properly. Tester class is***********

import java.util.*;
public class PlayPokerTest
{
public static void main(String args[])
{
int noOfSuit = 14;
String heart = "Heart";
String diamond = "Diamond";
String spade = "Spade";
String club = "Club";
Scanner in = new Scanner(System.in);
PlayPoker player = new PlayPoker();
for(int i=1; i<noOfSuit; i++)
{
player.addCards(heart,i);
}
player.showDeck();
player.cardsOfPlayer();
System.out.println("Player's Cards");
player.showAllotedCards();
player.cardsInDeck(5);
System.out.println("Now Cards in Deck");
player.showDeck();
String type = in.next();
int no = in.nextInt();
player.swap(type, no);
player.cardsToSwap("H1");
player.showDeck();
player.showAllotedCards();

}
}


*********One compilation error is mycode\PlayPokerTest.java:27: cardsToSwap(Card) in PlayPoker cannot be applied to (java.lang.String)
player.cardsToSwap("H1");
Please some help me in showing where is my code going wrong and how can i remove element from allotedCards arraylist ************
Chicon's Avatar
Computer Specs
Distinguished Member with 6,677 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
10-Nov-2007, 11:52 AM #2
Hi virkjay,

The cardsToSwap method of the PlayPoker class is expecting an object of Card type instead of a string.
You may replace the line :
player.cardsToSwap("H1");
by
player.cardsToSwap(new Card("H",1));
virkjay's Avatar
Junior Member with 10 posts.
 
Join Date: Nov 2007
Experience: Beginner
10-Nov-2007, 06:53 PM #3
hi, I changed that code according to Chicon, but now it is giving me a concurrent modification exception. I don't know y. Please help me.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,677 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
11-Nov-2007, 04:19 AM #4
The line allotedCards.remove(c); is wrong in the swap method of PlayPoker.
The remove(Object o) method of ArrayList already uses its own iterator to browse each item of the collection.
Therefore, it can't be used inside a for (Object o : array) loop that uses the same iterator.
It's the reason you're meeting a Concurrent Modification exception.

You may correct the method this way :
Code:

public void swap(String a, int b) // Problem with method in tester class
{
    for (int i = 0; i < allotedCards.size(); i++)
    {
        Card c = allotedCards.get(i);
        if((a.equals(c.getType()) && (b == c.getNumber()))
        {
            deck.add(c);
            allotedCards.remove(i);  
        }
    }
}
__________________
Never teach an old monkey how to make faces. - (French maxim)
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:51 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.