3 Replies - 613 Views - Last Post: 08 October 2015 - 11:11 AM Rate Topic: -----

#1 mbusa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 15

Exception Handling- Help with Try/ Catch

Posted 08 October 2015 - 10:40 AM

I have to run a program that does the following:
-Find the average grade of 10 students and number of students with the grade greater than the average.
-Add a try/catch block to ensure that while entering grades, a negative value is not encountered.
-Allow user to reenter the grade

I currently have the code to find the average and the number of students with the grade greater than the average. However I am so lost trying to use the try/catch block so that a negative value is not encountered and will allow the user to reenter if typed. My code is attached, can someone please help with the try and catch block? Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Exception Handling- Help with Try/ Catch

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Exception Handling- Help with Try/ Catch

Posted 08 October 2015 - 10:53 AM

you use something along the lines of
import java.util.*;

public class ReadInteger
{
public static void main(String[] args)
    {
     Scanner console = new Scanner(System.in);
     while (true)
        try
          {
          
           System.out.print("enter an integer ");
           int test = console.nextInt();                // read intger
           System.out.println("you entered " + test);   // display it
           break;
          }
        catch (Exception e)
          { System.out.println("Error! you entered '" + console.nextLine() + "' try again !"); }
      }
}


why do you think we need the console.nextLine() in the catch() statement

This post has been edited by horace: 08 October 2015 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 mbusa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-October 15

Re: Exception Handling- Help with Try/ Catch

Posted 08 October 2015 - 11:05 AM

View Posthorace, on 08 October 2015 - 10:53 AM, said:

you use something along the lines of
     Scanner console = new Scanner(System.in);
     while (true)
        try
          {
          
           System.out.print("enter an integer ");
           int test = console.nextInt();                // read intger
           System.out.println("you entered " + test);   // display it
           break;
          }
        catch (Exception e)
          { System.out.println("Error! you entered '" + console.nextLine() + "' try again !"); }
      }


why do you think we need the console.nextLine() in the catch() statement



Console.nextLine is needed because you want the integer you entered to display in the output. From looking at my code where Should I start the try block?
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Exception Handling- Help with Try/ Catch

Posted 08 October 2015 - 11:11 AM

View Postmbusa, on 08 October 2015 - 07:05 PM, said:

Console.nextLine is needed because you want the integer you entered to display in the output. From looking at my code where Should I start the try block?

no, the catch() is executed if console.nextInt() fails
the call to console.nextLine() is to remove the faulty characters from the input stream before attempting to read an integer again

you would add the try{} Catch(){} where you ask the user to enter the grades
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1