Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

vector problem  RSS feed

 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
Every Body when i compile and run the following exception
are throw by the interprter
please help me what is the mistake i have
i want to use vector to store data in file
i want to use vector to retrieve data from the file
this is my algrothim i have project from university to
establish data base i thing vector is best way to manipulate
data into or out of the file.



Thanks in advance!
ali //first input
ali // second input

ali // third input
ali // foruth input after i the following exception are throwm

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.setElementAt(Vector.java:482)
at Showingvector1.main(Showingvector1.java:37)
Press any key to continue . . .



import java.util.*;
import java.io.*;
class Add
{
String s;
String s1;
public Add(String m,String mn)
{
s=m+"\n";
s1=mn+"\n";
}
public String toString()
{
return s+s1;
} }
class Showingvector1
{
public static void main(String sm[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String st,st1;
FileOutputStream f=new FileOutputStream("jeay.txt");
DataOutputStream dt=new DataOutputStream(f);
Vector v=new Vector();int pos=0;
while(true){
st=br.readLine();
st1=br.readLine();
if (st=="Exit")
break;
Add ad=new Add(st,st1);
if(pos==0)
v.addElement(ad);
else
v.setElementAt(ad,pos);
Add ar=(Add)v.elementAt(pos);
dt.writeUTF(ar.s);
dt.writeUTF(ar.s1);
pos++;
} }


}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"1 >= 1" is one exception we all shake our heads at. It means position 1 exceeds the current size of the vector which is 1. Remember positions start counting at 0 so position 1 is the 2nd position.

When pos is zero you use addElement, resulting in a Vector with 1 element in it. Next you try to set position 1 and get the exception. You'll probably get what you want if you just use addElement all the time.

If you get this working and have some time, read up on the List interface and the ArrayList class. In general it's good to stop using the older Vector and start using the newer collections.
 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reaply if i set the current size of vector
by ten then also the exception
thrown
pleaaaaaaaaaaase help i have asignment to use only vector not other
then vector
 
Monarchy Javatheone
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will be better if you use
Vector v = new Vector(int anyintvalue);
So that the vector grows automatically without causing you
a problem
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor arguments set capacity, not current size. Create a new Vector with a large capacity and display the current size to see. Your setElementAt argument exceeds current size and gets the exception as promised by the JavaDoc. Do you have some reason to not use addElement() all the time?
 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply but idid not get it please help
 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks you have solved my problem thank aloooooooooooooot!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!