473,418 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Help with "ArrayIndexOutOfBoundsException" on code using Vector class and methods

Hello everyone,

I'm coding card functions (shuffle, draw, etc) using the Vector class and functions, and I'm almost finished except for an exception that I can't seem to get rid of.

Can anyone help me please? It's 2 lines of code which are causing the exception, and the problem is that I do not understand why it is throwing the exception, it should work.

Either it's them or my FOR function containing them is somehow flawed.

Lines are highlighted below: the last two add methods from Vector class.

//2.- reassemble pool and remaining deck on result deck , taking one turn each to effectively shuffle
deckSize = (deckV.size()*2) - 1;
System.out.println("Size of original deck is " + deckSize);
System.out.println("Size of deckV is " + deckV.size());
System.out.println("Size of deck2V is " + deck2V.size());
System.out.println("First element on deckV is " + deckV.get(0));
System.out.println("First element on deck2V is " + deck2V.get(0));
for (int i = 0; i < deckSize; i++) {

// EXCEPTION IS HERE!!!!!!!! ANY OF THESE TWO THROWS IT

deck3V.add(deckV.remove(0));
deck3V.add(deck2V.remove(0));
}

// print resulting reshuffled deck
System.out.println(" ");
System.out.println("Here begins reshuffled deck");
System.out.println("Size of resulting deck is " + poolV.size());
for (int i = 0; i < poolV.size(); i++) {
System.out.println(poolV.get(i));
}


}

}


Exception is (from printout):

Size of original deck is 51
Size of deckV is 26
Size of deck2V is 26
First element on deckV is 8 of Hearts
First element on deck2V is 2 of Clubs
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.remove(Unknown Source)
at can.McAfee.example1.Deck2.main(Deck2.java:137)
May 10 '10 #1
1 2415
jkmyoung
2,057 Expert 2GB
Do you have any guarantees as to the size of each of the 2 original decks?
DeckV and Deck2V

You'll definitely get an out of bounds error because the original first deck had size:
deckSize = deckV.size(), not
deckSize = (deckV.size()*2) - 1
May 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dan | last post by:
Hello, I am trying to read and write to an Excel file via my Java applet. I have done so successfully on several simple Excel files that simply had data cells without many complicated equations...
3
by: Herman | last post by:
Hi everyone, I have a multiple file project for a class assignment. It consists of a class Point, then a class Polygon which uses the Point object, then a class Picture that uses the Polygon...
7
by: Row | last post by:
Hi, Im relitivly new to java. I guess you could say im a newbie (even though i studied it 3 years ago) I have 2 simple questions. relating to java vectors Question 1: My application uses an...
1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
2
by: David Stevenson | last post by:
Programs from: Sheng Liang, The Java Native Interface, Programmer's Guide and Specification, The Java Series, (c) 1999, pp. 38-39. I thought I copied the program pretty exactly, but I don't know...
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
2
oll3i
by: oll3i | last post by:
Hi! i just started learning java ;] i wanna write bank accounts program i get an error i know its in login method but i dont know where :\ cd u please help me with this one the code below...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
2
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.