Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: WHy is it going straight to my exception?

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default WHy is it going straight to my exception?

     
    boolean markCheck = true;
         do{
           try{
            for (int i = 0; i <= amountAssignment-1; i++ ) {
              System.out.print("Homework Mark for Assignment " +(i+1) + ": ");
              homeworkMark[i] = Integer.parseInt(sc.nextLine());
              System.out.print("Examination Mark for Assignment " + (i+1) + ": ");
              examinationMark[i] = Integer.parseInt(sc.nextLine());
              markCheck = false;
            }
          }catch (Exception e) {
            System.out.println("------------");
            System.out.println("   ERROR    ");
            System.out.println("------------");   
          }
         }while (markCheck == true);

    After asking the mark for assignment 1 it goes directly to my exception without allowing to user to enter anything. After that it works fine!

    Cannot understand why

    -------------OUTPUT------------

    Student Number: 567
    Family name: Malcolm
    First Name: tanti
    Subjects: 1. Boat Maintenance
    2. Basic Sail Control
    3. Blue Water Navigation
    4. Chart Reading
    5. Laws and Customs of the sea

    Subject: 3
    How many assignments shall I calculate? : 2
    Homework Mark for Assignment 1: ------------
    ERROR
    ------------
    Homework Mark for Assignment 1: f
    ------------
    ERROR
    ------------
    Homework Mark for Assignment 1: 23
    Examination Mark for Assignment 1: 45
    Homework Mark for Assignment 2: 65
    Examination Mark for Assignment 2: 34


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: WHy is it going straight to my exception?

    Add a call to the printStackTrace() method in the catch block so the reason and location of the exception is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: WHy is it going straight to my exception?

    ok, i did that. no idea whats its on about though

    java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at RecordingCalculating.main(RecordingCalculating.jav a:35)
    at __SHELL16.run(__SHELL16.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:725 )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: WHy is it going straight to my exception?

    java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at RecordingCalculating.main(RecordingCalculating.jav a:35)
    The call to parseInt() on line 35 is passing it an empty String: ""
    One way to prevent that is have the program test the contents of the String before using it in a call to parseInt()
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: WHy is it going straight to my exception?

    ah, i get it.. so the number is being accepted as a string and parsed into an integer. The string is forcing it to go straight to the exception?

    It works with .nextInt();

    Thanks!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: WHy is it going straight to my exception?

    Using Scanner class methods has a problem with the end line character staying in the buffer. Mixing calls to nextInt() with calls to nextLine() will mean nextLine() reads an empty line as it clears the end line from the buffer.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 13
    Last Post: December 6th, 2011, 02:17 PM
  2. else if - else if - else if... Goes Straight To Final else
    By Omnamah in forum Loops & Control Statements
    Replies: 15
    Last Post: September 9th, 2011, 09:29 AM
  3. Getting my paddle to move left and right in a straight line-STUCK
    By warnexus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2011, 08:06 PM
  4. Straight and Straight Flush?!?!
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 10:44 PM