Could someone help me to do this assignemnt?? I have created FullDeck playing cards. with random number creation I am able to see the card but when i remove the card from the deck i am getting error message Indexoutofbounds exception.
I am not an expert in Java but know C and C++. Based on that the error message looks like something to do with your dynamic expansion of the array that you are using. The index that is being referred after removing (subtracting) is kind of getting exhausted beyond the lbound of the array. may be you want to check that? Esp. the array's lbound starts from zero and not one - right??. let me know if i am right
Thanks tuffy. I am new to programming trying very hard to learn things. I understand I need to reduce the index after removing it which i am doing too. Dont know how to move forward. The problem comes only after i add remove code. Yes u are right arrays start from 0-n. for(int x = 0; x < dl-1; ++x){ // int r = ((int)(Math.random() * 100)% cardsRemoved + 1);// System.out.println("random" + r);// myCard.add(deck[r]);// for (int a = 0; a < 1; ++a ){// myDeck.remove(r); // cardsRemoved = cardsRemoved-1;}// dl = dl-1;}// System.out.println("MyCard is "+ myCard); BTW I am also from TamilNadu, love our state soooo much. I am in Canada right now looking forward for the chance to visit TamilNadu.
I am not a Java person, I'm .NET, the potential problem in your code could be in the random number generator. I googled and found the range can be set with this formula RandomNumber = Min + (Math.random() * (Max - Min))Min + (Math.random() * (Max - Min)), in your case the Min should be 1 and Max should be 52. http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java Also when you say myDeck.remove(r), it removes the index I think. If your array length was 52 before (which means you had values from myDeck(0) to myDeck(51)), now after the myDeck.remove(r) statement, your array length is just 51, which means you have values from myDeck(0) to myDeck(50). So as a result, after your myDeck.remove(r) statement, if you try to access myDeck(51), you are going to get an index out of bounds exception. My suggestion is instead of removing the card, ie. myDeck.remove(r), do something like start a loop from r till the length of the array, and simply say myDeck(r) = myDeck(r+1), and after the loop remove the final index. Does that make sense? Just to summarize, the problem is you are dynamically reducing the size of the array (with the remove statement) and trying to access the old value of last index (51), which does not exist.
On flip side, if you choose to have the myDeck.remove(r) line, make sure to have another variable which maintains the size of the array, for example, the variable (lets call it arrSize) should be initialized with 52, and after myDeck.remove(r) statement, you should have arrSize = arrSize - 1. So when you run a loop on the index of the array make sure it ends with arrSize, because the size 52 is not relevant anymore after myDeck.remove(r) statement.
Hgulla, Many thanks. yes r is a random number, I am reducing the maximum random number by 1 in each step (cardsRemoved). One of the objective for the assignment is to remove a card from a random position and fill the position with next higher valued one. myDeck(r) = myDeck(r+1) with in a loop gives error message. In few weeks I am planing starting .NET course. How difficult is it. Now I am taking basics java and javascript. will I be able manage .Net with these basics. Thanks in advance.
See if you can print out the index every time and use a try/catch block to handle exceptions. That way you could know which index is triggering the exception. You are on the right path, learning Java and JavaScript etc. C#.NET conceptually is similar to Java, just different syntax. Just like Java has Java Virtual Machine, Main thread concepts, C# uses something called Common Language Runtime. C# is a lot easier to learn, Visual Studio is an amazing tool unlike Eclipse or NetBeans that we use for Java. But .NET becomes different when you start learning ASP.NET, WCF, Windows Services etc. It will be interesting/cool and those are easy to pick up on. After you learn JavaScript, try to focus on jQuery, jSON. CSS is a must have skill, at-least understanding part of it. Remember, Rome is not built in a day, so is mastering any technology. Having a good foundation is very important and you are building that now.