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

  • Home
  • SEARCH
  • 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 8160505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:15:47+00:00 2026-06-06T18:15:47+00:00

What if I want to take user input from the args[0] array, but just

  • 0

What if I want to take user input from the args[0] array, but just in case I (the user) forgot to define it, I wanted a prompt to come up – is it better to use an if block to determine whether the array item(s) is empty or not, or to catch the exception? So, is this

public class Stuff {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        String foo;
        if(args.length > 0) {
            foo = args[0];
        }
        else {
            foo = getString("Input? ");
        }
    }
    public static String getString(String prompt) {
        System.out.print(prompt + " ");
        String answer = input.nextLine();
        return answer;
    }   
}

better or worse than

public class Stuff {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        String foo;
        try {
            foo = args[0];
        }
        catch(ArrayIndexOutOfBoundsException e) {
            foo = getString("Input? ");
        }
    }
    public static String getString(String prompt) {
        System.out.print(prompt + " ");
        String answer = input.nextLine();
        return answer;
    }   
}
  • 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-06T18:15:49+00:00Added an answer on June 6, 2026 at 6:15 pm

    You need to test args.length rather than reading args[0]

    But apart from that error, it is better to use if/else for the following reasons:

    • It makes the code clearer
    • It has better performance (although not relevant in this case)
    • There is no exceptional condition (it is expected that the args array may be empty in some circumstances e.g. “user error”, so the code should be able to handle it). Exception throwing should be reserved for exceptional situations that shouldn’t happen.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirement is like: I want to take input values from user in frame but
I want to take Input from the user as Big-Integer and manipulate it into
I'm developing a winform application. I want to take input from user (i.e. the
I have a text box. I want to take user input from there and
I want to take the input from the user, which could be any of
I want to add a Customized UIView that will take Text input from user.
I want to have a form on my page take user input, a URL
Suppose i have array of characters. say char x[100] Now, i take input from
I want to learn that if I can define user input parameters at bean
I want to take person's name and their phone number from the end user

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.