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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:14:41+00:00 2026-05-28T01:14:41+00:00

I would like to know to know how do i compare the arguments passed

  • 0

I would like to know to know how do i compare the arguments passed into a java main method.

eg java hello -i

I tried printing args[0] and it does indeed gives me -i. however what i want to achieve is:

if args.length == 0 {
  do something
}
else if args[0] =="-i"{
  do something
}

However i keep getting index out of bound exception. is there anyway to convert the string in the string array args to just a string type so i can compare it?

public static void main(String[] args) {
  if args.length == 0 {
    do something
  }
  else if args[0] =="-i"
    do something
  } 


}

for example if they start the program without any arguments, i will call init() method. but if they entered -i as arguments, i will call install() instead..

  • 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-05-28T01:14:41+00:00Added an answer on May 28, 2026 at 1:14 am

    The code you’ve given wouldn’t even compile, but this does work:

    public class Test {
        public static void main(String[] args) {
            if (args.length == 0) {
                System.out.println("No arguments!");
            } else if (args[0].equals("-i")) {
                System.out.println("-i passed");
            } else {
                System.out.println("Something else");
            }
        }
    }
    

    Note that it’s important that you’re using else here – this would fail, for example:

    if (args.length == 0) {
        System.out.println("No arguments!");
    }
    if (args[0].equals("-i")) {
        System.out.println("-i passed");
    }
    

    at that point you’re checking args[0] even if the length of the array is 0. Given that you’ve given pseudo-code at the moment (no brackets round the conditions) I wonder whether that’s the problem in your real code.

    (Also note the use of equals instead of == as others have already pointed out.)

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

Sidebar

Related Questions

I would like to know how many machine cycles does it take to compare
I would like to know if there is anyway I can compare two columns
I would like to know how to compare two two-dimension arrays value. First array
I would like to know that if I can compare a value with a
I would like to know if using VBA I can compare two similar excel
I would like to know what is better to use. IComparer class and Compare
I'd like to know what would be the best strategy to compare a group
I am able to compare Strings fine, but would like to know how I
I would like to know if I can compare 2 member functions with the
I would like to know what is the fastest way in java 1.5 to

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.