I am doing a project in class. We have to make a program that scans Two Postive Integers, one at a time, using try-catch block. Can some one please help me get started?

You have to first create a class that inherits the Exception class because that is how a try-catch block is implemented.Throw an error if the input number is less than zero.
Hope you have been taught exception handling.

But if it weren't for an assignment,seriously,I do not get the point of doing this in a try catch block.

delta frost: any reason why the already existing Exception classes are not sufficient?
the point might be, that in the future he might need to use a readDigit method to input data, which throws an exception in case of invalid input.

This is what i have so far, Im trying to ger it to read the first integer, but it is not catching the exception. We have to leave at least one attempt for guessing the second integer. If someone can help me withought using the try-catch, that would be great also.

package readtwopostiveinteger;
import java.util.*;
import java.io.*;


public class ReadTwoPostiveInteger {
    private static final int numAttempts=5;
    private static int posInt;

    public static void main(String[] args) {
     int i;
     System.out.println("Enter two postive integers,one at a time, # of Attemps Remaining:"+numAttempts);
     Scanner keyb = new Scanner(System.in);

     for(i= 1; i<=numAttempts-1; i++) {
         try { posInt = keyb.nextInt();
              if (posInt>0)
              System.out.println("Correct");
              break;

                   }
           catch(InputMismatchException ime){

                System.out.println("Your Input"+posInt+"was not a positive integer");
                if (i== numAttempts) System.out.println ("sorry");
                else System.out.println("Enter a postive integer,#of attemps remaining"+numAttempts);
                continue;
                 }



             }



            }

         }

if you read a negative number, it 'll not throw an exception. it will just not execute the print statement, and go straight into the break statement.

I removed the break and it still dies not catch.. I also don't know how to go about reading the second integer.

it still doesn't catch, because you still don't throw an exception. the catch will only be executed, if there is an exception to be caught.

I dont understand, How am I not throwing an exception?

.Throw an error if the input number is less than zero.

You missed this part out. The catch is OK, but you don't throw the exception when the input is wrong.

I dont understand, How am I not throwing an exception?

eg

if (! ans.equals("y") || ans.equals("n")) throw new InputMismatchException("must be y or n");

Ok I fixed it, but now the number of Attempts is not decreasing. The user should only get 5 attempts to guess to postive integers. Also I need to leave one attempt for the second integer, For example if the first 4 attempts are not postive integers, The user can still get the second one right.

package readtwopostiveinteger;
import java.util.*;
import java.io.*;


public class ReadTwoPostiveInteger {
    private static final int numAttempts=5;
    private static int posInt;

    public static void main(String[] args) {
     int i;
     System.out.println("Enter two postive integers,one at a time, # of Attemps Remaining:"+numAttempts);
     Scanner keyb = new Scanner(System.in);

     for(i= 1; i<=numAttempts-1; i++) {
         try { posInt = keyb.nextInt();
              if (posInt>0)
              System.out.println("Correct");
               if (posInt<=0) throw new InputMismatchException();          
                   }
           catch(InputMismatchException ime){

                System.out.println("Your Input was not a positive integer");
                if (i== numAttempts) System.out.println ("sorry");
                else System.out.println("Enter a postive integer,#of attemps remaining:" +numAttempts);
                continue;
                 }



             }



            }



     }

Someone please help

You may wish to decrement the number of attempts at some stage.
If you want to decrement it when you have received a valid input then you will need to make the if condition body a multi-statement one. (Your current condition does not enclose the statement in braces so therefore only one statement can be executed if the condition is true.)

if (posInt>0)
{
    System.out.println("Correct");
    numAttempts--;
}

Of course, you might want to decrement numAttempts at a different stage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.