Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:48:01+00:00 2026-06-14T07:48:01+00:00

I try to catch the exception which finally works but now I need to

  • 0

I try to catch the exception which finally works but now I need to figure out how to make it loop. ALso, how would I be able to get user input(int select) to the outside of the loop? I can try creating a new function, that might do the trick.

do {
    System.out.print("How many integers shall we compare? (Enter a positive integer): ");
    try {
        select = intFind.nextInt();
    } catch (InputMismatchException e) {
        // Display the following text in the event of an invalid input
        System.out.println("Invalid input!");
        intFind.nextLine();
    }
} while (select < 0);

Then

do
            {
                System.out.print("How many integers shall we compare? (Enter a positive integer): ");
                try {
                    b = Get();
                }
                catch (InputMismatchException e) {
                    // Display the following text in the event of an invalid input
                    System.out.println("Invalid input!");

                    intFind.nextLine();
                } copySel = b;  
            }while(select < 0);

and

static int Get()
    {
        Scanner intFind = new Scanner(System.in);
        int select;
        select = intFind.nextInt();
        return select;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:48:02+00:00Added an answer on June 14, 2026 at 7:48 am

    To make it loop make sure that select is initialized to a value less than 0. But it would be better to have a condition select < 2, because it makes more sense to compare at least to values.

    Create a method like

    private int[] promptIntegers(int numberOfIntegers, Scanner sc) {
        int[] integers = new int[numberOfIntegers];
        //read ints
        //return the array
    }
    

    and call it like

    select = intFind.nextInt();
    int[] integers = promptIntegers(select, sc);
    //do the comparison among the integers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can fix the below exception with a try-catch loop but I cannot understand
I have the code below which I now need to figure out how to
I understand how try-catch works and how try-finally works, but I find myself using
try { // Code } catch (Exception ex) { Logger.Log(Message, ex); throw; } In
Can you try/catch a stack overflow exception in java? It seems to be throwing
try{Integer.parse(Abhishek);} catch(NumberFormatException e){} catch(Exception e){} If for the above piece of code, if NumberFormatException
I'm trying to throw an exception (without using a try catch block) and my
I have this snipped code: try{ JAXB.xmlJavaConverter(clientCommand); } catch (Exception e){ nServerFifo.add(clientCommand); } I
Background: Suppose I have the following obviously-incorrect PHP: try{ $vtest = ''; print(array_pop($vtest)); }catch(Exception
Why can't I catch this exception? My (client) code: Eigen::MatrixXd FFs ; try {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.