help with Vector class for searching and object manipulation

This is part of a project but I don't understand the errors. about Vector.toArray and Vector.get()

they should be working.

Thanks.

mport java.awt.event.*;

import javax.swing.*;

import java.util.*;

class Libritos

{

privatestatic Vector empleados =new Vector();

privatestatic Vector libros =new Vector();

privatestatic Vector clientes =new Vector();

privatestatic Vector facturas =new Vector();

publicstaticvoid main(String[] args)

{

Gui Ventana =new Gui();

}

publicstatic Vector buscarLibro(String value)

{

Vector lista =new Vector();

int temp;

Libro arreglolibros[]=libros.toArray();

for (int i=0; i<arreglolibros.getLength();i++)

{

if(arreglolibros[i].titulo.matches(value))

{

lista.add(libros.elementAt(i));

}

}

return lista;

}

// begin setget

publicstaticvoid nuevoEmpleado()

{

empleados.add(new Empleado());

}

publicstaticvoid nuevoLibro()

{

libros.add(new Libro());

}

publicstaticvoid nuevoCliente()

{

clientes.add(new Cliente());

}

publicstatic Empleado getEmpleado(int value)

{

Empleado temp = empleados.get(value);

return temp;

}

publicstatic Cliente getCliente(int value)

{

return clientes.elementAt(value);

}

publicstatic Factura getFactura(int value)

{

return facturas.elementAt(value);

}

publicstatic Libro getLibro(int value)

{

return libros.elementAt(value);

}

}

And the error:

javac Libritos.java

Libritos.java:28: incompatible types

found: java.lang.Object[]

required: Libro[]

Libro arreglolibros[]=libros.toArray();

^

Libritos.java:30: cannot find symbol

symbol : method getLength()

location: class Libro[]

for (int i=0; i><arreglolibros.getLength();i++)

^

Libritos.java:33: titulo has private access in Libro

if(arreglolibros.titulo.matches(value))

^

Libritos.java:76: incompatible types

found: java.lang.Object

required: Empleado

Empleado temp = empleados.get(value);

^

Libritos.java:84: incompatible types

found: java.lang.Object

required: Cliente

return clientes.elementAt(value);

^

Libritos.java:91: incompatible types

found: java.lang.Object

required: Factura

return facturas.elementAt(value);

^

Libritos.java:98: incompatible types

found: java.lang.Object

required: Libro

return libros.elementAt(value);

^

./Factura.java:33: cannot find symbol

symbol : variable True

location: class Factura

ventana.setVisible(True);

^

Note: ./Gui.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

Note: Some input files use unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

8 errors>

[5649 byte] By [--D_S--a] at [2007-10-3 6:02:55]
# 1
A Vector returns an Object[] when you call toArray. The code does not know that the vector only contains instances of Libro. You need to use generics, or add casts.Kaj
kajbja at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 2

Error 1:

/Factura.java:33: cannot find symbol

symbol : variable True

location: class Factura

ventana.setVisible(True);

^

Solution:

use the lowercase "true"

ventana.setVisible(true);

Error 2:

Libritos.java:30: cannot find symbol

symbol : method getLength()

location: class Libro[]

for (int i=0; i><arreglolibros.getLength();i++)

^

Soluion:

1) What is the i>< ?

2) Check whether the getLength() exists in class Libro. If not use

for (int i=0; i<arreglolibros.getLength();i++)

Error 3:

Libritos.java:28: incompatible types

found : java.lang.Object[]

required: Libro[]

Libro arreglolibros[]=libros.toArray();

Solution:

the toArray() is returning an Object. You are trying to assign directly to Libro. Any class is a subclass of Object class. Base class object cant be assigned to a subclass. Try to find a workaround.>

FebTena at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks, this is my first java program but I still don't get quite the hang of it.How can I apply these to the code?I'm stuck here I need these to contnue as I need to search the libros Vector to add the Libro objects to another class.
--D_S--a at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 4
> Soluion:> 1) What is the i>< ? That's a bug in this forum's rendering software.
TimRyanNZa at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 5
> How can I apply these to the code?Add generics.E.g. Vector<Libro> libros = new Vector<Libro>();
kajbja at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 6

Ok getting better

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

class Libritos

{

private static Vector<Empleado> empleados = new Vector<Empleado>();

private static Vector<Libro> libros = new Vector<Libro>();

private static Vector<Cliente> clientes = new Vector<Cliente>();

private static Vector<Factura> facturas = new Vector<Factura>();

public static void main(String[] args)

{

Gui Ventana = new Gui();

}

public static Vector buscarLibro(String value)

{

Vector lista = new Vector();

int temp;

Libro arreglolibros[]=libros.toArray();

for (int i=0; i < arreglolibros.getLength(); i++)

{

if(arreglolibros[i].getTitulo().matches(value))

{

lista.add(libros.elementAt(i));

}

}

return lista;

}

// begin setget

public static void nuevoEmpleado()

{

empleados.add(new Empleado());

}

public static void nuevoLibro()

{

libros.add(new Libro());

}

public static void nuevoCliente()

{

clientes.add(new Cliente());

}

public static Empleado getEmpleado(int value)

{

Empleado temp = empleados.get(value);

return temp;

}

public static Cliente getCliente(int value)

{

return clientes.elementAt(value);

}

public static Factura getFactura(int value)

{

return facturas.elementAt(value);

}

public static Libro getLibro(int value)

{

return libros.elementAt(value);

}

}

Libritos.java:28: incompatible types

found: java.lang.Object[]

required: Libro[]

Libro arreglolibros[]=libros.toArray();

^

Libritos.java:30: cannot find symbol

symbol : method getLength()

location: class Libro[]

for (int i=0; i < arreglolibros.getLength(); i++)

^

Note: ./Gui.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

Note: Some input files use unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

2 errors

the second is because of the first?

--D_S--a at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 7

> Libritos.java:28: incompatible types

> found: java.lang.Object[]

> required: Libro[]

> Libro arreglolibros[]=libros.toArray();

>

Change that line to:

Libro arreglolibros[]=libros.toArray(new Libro[0]);

>

> symbol : method getLength()

> location: class Libro[]

> for (int i=0; i < arreglolibros.getLength();

> i++)

There is no method getLength on an array, just use arreglolibros.length instead.

> the second is because of the first?

No, see above.

kajbja at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 8

Now that the vector return is solved is it better this?

public static Vector buscarLibro(String value)

{

Vector lista = new Vector();

int temp;

// Libro arreglolibros[]=libros.toArray();

for (int i=0; i < libros.indexOf(libros.lastElement()); i++)

{

if(libros.get(i).getTitulo().matches(value))

{

lista.add(libros.elementAt(i));

}

}

return lista;

}

--D_S--a at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 9
> for (int i=0; i <> libros.indexOf(libros.lastElement()); i++)What do you mean by that? Why not just use libros.size() ?Kaj
kajbja at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...
# 10
In case an element is removed but I think that I'll leave that option out.By the way thanks a lot this was critical for the rest of the project.
--D_S--a at 2007-7-15 0:45:13 > top of Java-index,Java Essentials,New To Java...