2 Replies Latest reply: Jul 2, 2011 8:49 AM by 872842 RSS

    Exception

    872842
      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
        • 1. Re: Exception
          798692
          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
          ....
          ....
          • 2. Re: Exception
            872842
            Thanks lot......................