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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:31:15+00:00 2026-06-13T01:31:15+00:00

I’m pretty new to java and I have nested Try Catch code that prints

  • 0

I’m pretty new to java and I have nested Try Catch code that prints 3 times! can you tell me why?
I get 3 int from Command Line and has to be validated and check if they in range and i am using try and catch. but the answers prints 3 times!

// Global Constants
final static int MIN_NUMBER = 1;
final static int MAX_PRIME = 10000;
final static int MAX_FACTORIAL = 12;
final static int MAX_LEAPYEAR = 4000;

// Global Variable
static int a, b, c;

public static void main(String[] args) {
    // String[] myNumbers= new String [3];

    // int x =Integer.parseInt(args[0]);
    for (int i = 0; i < args.length; i++) {
        // System.out.print(args[i]+" ");
        validateInput(args[0], args[1], args[2]);

    }
}

// Validate User Input
public static boolean validateInput(String value1, String value2, String value3) {
    boolean isValid = false;

    try {
        try {
            try {
                a = Integer.parseInt(value1);
                if (!withinRange(a, MIN_NUMBER, MAX_PRIME)) {
                    System.out.println(
                            "The entered value " + value1
                            + " is out of range [1 TO 10000].");
                }
                isValid = true;         

            } catch (Exception ex) { 
                System.out.println(
                        "The entered value " + value1
                        + " is not a valid integer.  Please try again.");
            }   

            b = Integer.parseInt(value2);
            if (!withinRange(b, MIN_NUMBER, MAX_FACTORIAL)) {
                System.out.println(
                        "The entered value " + value2
                        + " is out of range [1 TO 12].");
            }
            isValid = true; 
        } catch (Exception ex) { 
            System.out.println(
                    "The entered value " + value2
                    + " is not a valid integer.  Please try again.");
        }

        c = Integer.parseInt(value3);
        if (!withinRange(c, MIN_NUMBER, MAX_LEAPYEAR)) {
            System.out.println(
                    "The entered value " + value3
                    + " is out of range [1 TO 4000].");
        }
        isValid = true; 
    } catch (Exception ex) { 
        System.out.println(
                "The entered value " + value3
                + " is not a valid integer.  Please try again.");
    }   

    return isValid;

}

// Check the value within the specified range
private static boolean withinRange(int value, int min, int max) {
    boolean isInRange = true; 

    if (value < min || value > max) {
        isInRange = false; 
    }
    return isInRange;
}
  • 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-13T01:31:16+00:00Added an answer on June 13, 2026 at 1:31 am

    The answer is printed 3 times, since you have put the validateInput method call into a for loop in main(), which runs it three times in case of three arguments.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.