Foros del Web » Programación para mayores de 30 ;) » Java »

Exception in thread "main" java.util.NoSuchElementException

Estas en el tema de Exception in thread "main" java.util.NoSuchElementException en el foro de Java en Foros del Web. Hola a todos me sale el siguiente error cuando compilo un codigo de java sdk en eclipse Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) ...
  #1 (permalink)  
Antiguo 20/01/2009, 16:48
 
Fecha de Ingreso: enero-2009
Mensajes: 7
Antigüedad: 15 años, 2 meses
Puntos: 0
Exception in thread "main" java.util.NoSuchElementException

Hola a todos me sale el siguiente error cuando compilo un codigo de java sdk en eclipse

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at stock.elimina_stockHerramienta(manejaTool.java:83)
at manejaTool.main(manejaTool.java:161)

el codigo es el siguiente:

class stock{
private String tipo, codigo, cantidad, nombre;
private int cantMinima;



public stock(){}


public String consulta_stockHerramienta(String nom)throws java.io.IOException,java.io.FileNotFoundException{
String str = new String();
BufferedReader lee;
lee=new BufferedReader(new FileReader("stock.dat"));

while( (str=lee.readLine() )!=null){
StringTokenizer strtk=new StringTokenizer(str, "/");

for(int i=0; strtk.hasMoreTokens(); i++){
codigo = strtk.nextToken();
tipo=strtk.nextToken();
nombre=strtk.nextToken();

if(nombre.compareTo(nom)==0)
return str;
}
}
return "0";
}

public void elimina_stockHerramienta(String nom)throws java.io.IOException,java.io.FileNotFoundException{
String str1=consulta_stockHerramienta(nom);
StringTokenizer strtk1=new StringTokenizer(str1,"/");

codigo = strtk1.nextToken();
tipo = strtk1.nextToken();
nombre = strtk1.nextToken();
cantidad = strtk1.nextToken();

int cant=Integer.parseInt(cantidad);
cant=cant-1;
cantidad = String.valueOf(cant);

str1=codigo.concat("/");
str1=str1.concat(tipo);
str1=str1.concat("/");
str1=str1.concat(nombre);
str1=str1.concat("/");
str1=str1.concat(cantidad);
//System.out.println(str1);

stock aux[]=new stock[100];
String str2 = new String();
String cod = new String();
String tip = new String();
String nomb = new String();
String canti = new String();
BufferedReader lee;
lee=new BufferedReader(new FileReader("stock.dat"));
//System.out.println(codigo);

while( (str2=lee.readLine() )!=null){
StringTokenizer strtk=new StringTokenizer(str2, "/");


for(int i=0; strtk.hasMoreTokens(); i++){
cod=strtk.nextToken();

if(cod.equals(codigo)==true)
continue;
tip=strtk.nextToken();
nomb=strtk.nextToken();
canti=strtk.nextToken();
//System.out.println(cod);
/*aux[i].codigo=cod;
aux[i].tipo=tip;
aux[i].nombre=nomb;
aux[i].cantidad=canti;
System.out.println(aux[i]);*/
}



}

/*FileWriter fw= new FileWriter("stock.dat");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter salida=new PrintWriter(bw);
salida.println(str1);
salida.close();
String str2=new String();

for(int i=0; i<aux.length; i++){
str2=aux[i].codigo.concat("/");
str2=str2.concat(aux[i].tipo);
str2=str2.concat("/");
str2=str2.concat(aux[i].nombre);
str2=str2.concat("/");
str2=str2.concat(aux[i].cantidad);

FileWriter fw2= new FileWriter("stock.dat",true);
BufferedWriter bw2 = new BufferedWriter(fw2);
PrintWriter salida2=new PrintWriter(bw2);
salida.println(str2);

}*/

}



}

class herramienta{
private String tipo, codigo, responsable, cantidad;


public int registra_prestamo(String cod, String tip, String resp, String cant)throws java.io.IOException,java.io.FileNotFoundException{
FileWriter fw= new FileWriter("prestamo.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter salida=new PrintWriter(bw);
//BufferedWriter sal=new BufferedWriter(new FileWriter("prestamo.txt", true));
String str=new String();
str=cod.concat("/");
str=str.concat(tip);
str=str.concat("/");
str=str.concat(resp);
str=str.concat("/");
str=str.concat(cant);
int largo=str.length();

salida.println(str);

salida.close();
System.out.println(str);
return 1;
}
}


public class manejaTool{

public static void main(String[] args)throws java.io.IOException,java.io.FileNotFoundException{
stock obj=new stock();
//herramienta obj2=new herramienta();
//herramienta obj3=new herramienta();
//obj2.registra_prestamo("108", "martillo", "david", "2");
//obj2.registra_prestamo("110", "picota", "roberto","4");
String num=new String();
String s="martillo";
obj.elimina_stockHerramienta(s);
//System.out.println(num);
}
}

lo copie todo, pero el error ocurre cuando agrege el if que esta en rojo, antes tenia el if(cod==codigo), pero son objetos de tipo String y necesito compararlos.
  #2 (permalink)  
Antiguo 21/01/2009, 02:57
 
Fecha de Ingreso: octubre-2007
Mensajes: 28
Antigüedad: 16 años, 4 meses
Puntos: 0
Respuesta: Exception in thread "main" java.util.NoSuchElementException

el error te lo esta dando xq se queda sin tokens el stringtokenizer, te propongo que cambies el tipo de bucle en los stringtokenizer, al menos he visto dos bucles del stringtokenizer, los tienes con un for(int i=0; strtk.hasMoreTokens(); i++), pues mi idea es que lo cambies por un
while (strtk.hasMoreTokens()).

Salu2
  #3 (permalink)  
Antiguo 22/11/2010, 16:36
 
Fecha de Ingreso: enero-2010
Mensajes: 20
Antigüedad: 14 años, 2 meses
Puntos: 1
Respuesta: Exception in thread "main" java.util.NoSuchElementException

Te recomiendo que imprimas todos los tokens de tu StringTokenizer, para que veas que o cuales tokens son los que tienes, por ejemplo: while (strtk.hasMoreTokenz) {
System.out.println(tknz.nextToken);} para que puedas darte cuenta si estas o no tratando de asignar valores con mas tokens de los que hay. Espero sirva de algo, saludos!
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:02.