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 8433647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:26:42+00:00 2026-06-10T06:26:42+00:00

I am writing a program that accepts two ints within the program using nextInt();

  • 0

I am writing a program that accepts two ints within the program using nextInt(); and have it wrapped in a try catch block to stop bad inputs such as doubles or chars.

When multiple wrong inputs are entered the loop repeats that same number of times. I assume this is because my scan.next() has to loop around enough times to catch the bad inputs w/o error. Is there a way to know this number on the first run through to make a loop to run next in that many times?

In the output the
if(cont == 'N') System.out.print("\nPlease re-enter\n\t:"); will output and mirror the amount of times a mismatched input was written. That is, if I input 3 3.3 it will repeat one extra time, if input s 3.3 2.5 it will repeat three extra times.

I tried putting a loop around scan.next() to default it to ten times, but was overboard and I had to input an extra 8 characters before it started reading again. Maybe a while loop but what would its condition be, I tried while(scan.next() != null){} but that condition never stopped.

//input error checking
char cont = 'Y';
do{
  if(cont == 'N')
    System.out.print("\nPlease re-enter\n\t:");
  cont = 'Y';
  /* to stop the accidential typing of things other
   * than integers from being accepted
   */
  try{
    n1 = scan.nextInt();
    n2 = scan.nextInt();
  }catch(Exception e){
    cont = 'N'; //bad input repeat loop
    scan.next();//stops infinite loop by requesting Scanner try again
  }
} while(cont == 'N');//do loop while told N for continue
  • 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-10T06:26:43+00:00Added an answer on June 10, 2026 at 6:26 am

    Not sure what you want your code to do. From reading what you have posted I assume you want the user to input 2 ints and if he/she doesn’t you want to prompt him/her to re-enter something until he/she inputs 2 ints.
    If this is the case I would just add

    scan = new Scanner(br.readLine());

    after this if statement:

    if(cont == 'N')
    {System.out.print("\nPlease re-enter\n\t:");}

    This will solve your looping issue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a C program that accepts input one line at a time from
I am writing a program in c++ that accepts a filename as an argument
I'm writing a program in Python that accepts command line arguments. I am parsing
I'm writing an autograder web application that accepts a program from the user as
I'm writing a program that uses the Qt Graphics View framework. I have subclassed
I have two separate processes: a C program that outputs comma separated values followed
I'm beginner to c++ and writing a program that accepts user choices and acts
I have a small server program that accepts connections on a TCP or local

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.