Forum Stats

  • 3,837,113 Users
  • 2,262,228 Discussions
  • 7,900,208 Comments

Discussions

Exception

872842
872842 Member Posts: 9
PLz be kind enough to help me to find the answer.

String csv="Sue,5,true,3";
Scanner scanner=new Scanner(csv);
scanner.useDelimiter(",");
int age=scanner.nextInt();


Answer tells that an exception is thrown in run time.
Plz explain What is the reason for that?

regards ,
Minuri...

Edited by: 869839 on Jul 2, 2011 6:11 AM

Answers

  • 798692
    798692 Member Posts: 433
    First thing is use code tag to post your code.
    869839 wrote:
    String csv="Sue,5,true,3";
    Scanner scanner=new Scanner(csv);
    scanner.useDelimiter(",");
    int age=scanner.nextInt();
    You can find the reason for the errors from the Exception message. Here in your case the exception message is java.util.InputMismatchException

    Values from the scanner are in the order Sue, 5, true, 3 respectively. First value is a String and you are trying to read that as Integer. So read all the value one by one like,
    String s = scanner.next();        // First String Value
    int age = scanner.nextInt();     // Second int Value
    ....
    ....
    798692
  • 872842
    872842 Member Posts: 9
    Thanks lot......................
This discussion has been closed.