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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:44:37+00:00 2026-06-12T10:44:37+00:00

For school I was creating a simple Geometry program asking for the number of

  • 0

For school I was creating a simple Geometry program asking for the number of corners and coordinates of a shape. To prevent wrongfull entry (ie chars instead of integers) I figured I’d use exception handling. It seems to work fine, but once I have a wrong input it catches the error and set some default values. It should continue asking for more input, but somehow these end-up catching the same exception without asking for new input.

public static void main(String[] args) {
   Scanner stdin = new Scanner(System.in);

    try {
        int hoeken;
        System.out.print("How many corners does your shape have? ");

        try {
            hoeken = stdin.nextInt();
            if(hoeken < 3) throw new InputMismatchException("Invalid side count.");
        }
        catch(InputMismatchException eVlakken){
            System.out.println("Wrong input. Triangle is being created");
            hoeken = 3;
        }

        Veelhoek vh = new Veelhoek(hoeken);
        int x = 0, y = 0;

        System.out.println("Input the coordinates of your shape.");
        for(int i = 0; i < hoeken; i++){
            System.out.println("Corner "+(i+1));                
            try {
                System.out.print("X: ");
                x = stdin.nextInt();
                System.out.print("Y: ");
                y = stdin.nextInt();                    
            } catch(InputMismatchException eHoek){
                x = 0;
                y = 0;
                System.out.println("Wrong input. Autoset coordinates to 0, 0");
            }                
            vh.setPunt(i, new Punt(x, y));
        }

        vh.print();

        System.out.println("How may points does your shape needs to be shifted?");
        try {
            System.out.print("X: ");
            x = stdin.nextInt();
            System.out.print("Y: ");
            y = stdin.nextInt();

        } catch(InputMismatchException eShift){
            System.out.println("Wrong input. Shape is being shifted by 5 points each direction.");
            x = 5;
            y = 5;
        }            

        vh.verschuif(x, y);
        vh.print();

    } catch(Exception e){
        System.out.println("Unknown error occurred. "+e.toString());
    } 
}

Thus if the user starts of by trying to create a shape with 2 sides or inputs a char instead of an integer it produces the InputMismatchException and handles it. It then should continue the program by asking the coordinates of the corners, but instead it keeps throwing new exceptions and handles does.

What is going wrong?

  • 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-12T10:44:39+00:00Added an answer on June 12, 2026 at 10:44 am

    According I the API you need to skip the error ous entry to get the next one .
    “When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.”

    Calling the skip() method after getting the exception should fix this.
    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

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

Sidebar

Related Questions

I am creating a mine sweeper program in java for school, and am having
I'm creating for school a multiple choice program. For this I have to write
I am creating a data entry form for a school. The school will be
I am creating a simple Mahjong game for a final project at school and
I'm creating an iOS project for school and it works with chemical reactions. The
I'm creating a old school music emulator for the old GWBasic PLAY command. To
I am creating a system for my dissertation for a school to use. There's
i'm building a simple school portal, i have stucked at uploading an image into
Here is a simple model: class TakingCourse(models.Model): course = models.ForeignKey(Course) term = models.ForeignKey(Term) Instead
I am trying to design a database for creating a school syllabus. In a

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.