| Autor |
Mensagem |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 17/07/2009 18:08:49
|
marcusbiava
JavaBaby
Membro desde: 04/05/2009 13:57:22
Mensagens: 93
Localização: Floripa
Offline
|
String csv = ?Sue,5,true,3?;
Scanner scanner = new Scanner( csv);
scanner.useDelimiter(?,?);
int age = scanner.nextInt();
Porque esta lançando InputMismatchException no nextInt??
E o que o nextInt realmente faz????
|
|
|
 |
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 17/07/2009 18:11:53
|
marcusbiava
JavaBaby
Membro desde: 04/05/2009 13:57:22
Mensagens: 93
Localização: Floripa
Offline
|
Documentação:
nextInt
public int nextInt(int radix)
Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
If the next token matches the Integer regular expression defined above then the token is converted into an int value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via Character.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to Integer.parseInt with the specified radix.
Parameters:
radix - the radix used to interpret the token as an int value
Returns:
the int scanned from the input
Throws:
InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
Mas mesmo assim não ficou claro para mim.
|
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 18/07/2009 15:15:30
|
Cris Finholdt
Debugger
![[Avatar]](/images/avatar/cc1be2756ee268969328208597b5bd90.jpg)
Membro desde: 16/04/2009 10:25:19
Mensagens: 56
Offline
|
o método nextInt() = obtem o proximo token, retorna o valor do tipo int, e em seguida se move para o token seguinte.
No seu codigo, int age = scanner.nextInt(); está retornando o valor do primeiro token, que no caso é "Sue", uma String e nao um int, por isso ele lança a excessão. Mas se o valor do primeiro token fosse um int, por exemplo: String csv = "6, Sue,5,true,3"; , entao nao seria lançada nenhuma excessão e o valor de age seria 6. Entendeu? Nao sou mto boa pra explicar, mas eh isso.
Se vc quiser usar essa String csv = "Sue,5,true,3"; o metodo a ser usado, deveria ser next(); que faz a mesma coisa que o metodo nextInt() só que ao invés de retornar um int, retorna uma String.
This message was edited 2 times. Last update was at 18/07/2009 15:19:04
|
Cristiane Brandão Cobo
Analista Programadora
Sun Certified Java Programmer 6.0
Oracle Web Components Developer 5.0
Bacharel em Sistemas de Informação - UNIUBE |
|
|
 |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 18/07/2009 16:19:58
|
marcusbiava
JavaBaby
Membro desde: 04/05/2009 13:57:22
Mensagens: 93
Localização: Floripa
Offline
|
Muito obrigado Cris Finholdt
|
|
|
 |
|
|
|
|