|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2006
Messaggi: 7413
|
[Java] Problema con un Delimiter di uno Scanner
Salve ragazzi! Ipotizzando di dover leggere con uno Scanner da un File un oggetto composto da: int:int:int-int:int avevo pensato di fare una cosa così:
Codice:
public static Object read(Scanner s) throws Exception{
if(s.hasNextLine()){
int giorno_ = s.useDelimiter(":").nextInt();
int mese_ = s.useDelimiter(":").nextInt();
int anno_ = s.useDelimiter("-").nextInt();
int ora_ = s.useDelimiter(":").nextInt();
int minuto_ = s.nextInt();
if(s.hasNextLine()) s.nextLine();
return new Object(giorno_,mese_,anno_,ora_,minuto_);
}
else return null;
}
Codice:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Object.read(Object.java:42) at Main.main(Main.java:12) Come posso risolvere?
__________________
If you wanna make the world a better place, take a look at yourself and then make a change. MJ |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2001
Città: Milano
Messaggi: 545
|
quell'eccezione viene lanciata in realtà da .nextInt()
ti conviene quindi controllare che il contenuto del file sia corretto
__________________
Angus the Hunter @ Realm of magic | Angus Young @ Batracer °SetiEmperor°| Ninja Technologies { qualunque cosa sia, è veloce e fa male (cit.) } |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: May 2006
Messaggi: 7413
|
Uhm....
Il file è fatto solamente da numeri interi (ad esempio: 10:04:2008-10:30) Se provo a mettere al posto del "-" nuovamente i ":" non ho problemi
__________________
If you wanna make the world a better place, take a look at yourself and then make a change. MJ |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Oct 2004
Messaggi: 159
|
Codice:
public static Oggetto read(String s) {
String[] tokens = s.split(":|-");
return new Oggetto (tokens[0],tokens[1],tokens[2],tokens[3],tokens[4]);
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:42.



















