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

The Archive Base Latest Questions

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

I was thinking about this and wondering if it is possible for a string

  • 0

I was thinking about this and wondering if it is possible for a string and an integer to be on the same input line. For example, the user puts in “A=2”, “B=3”, and “C= A+B”, with C equaling 5. If so, what type of technique would I have to know? What would I have to look up?

  • 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-15T10:04:45+00:00Added an answer on June 15, 2026 at 10:04 am

    If you’re trying to detect command line arguments then I would consider something like this approach:

    First you’ll want to make sure the user actually inputs some amount of arguments:

    public static void main(String[] args)
    {
         if(args.length > 0)
         {
              //Check each set of arguments.
         }
         else
         {
              System.out.println("Invalid number of arguments");
              System.exit(1);
              // Here you can either do a try-catch and throw an exception or just simply exit if the user doesn't input the `enter code here`correct number of arguments. 
         }
    }
    

    The tricky part will be determining whether the user has input A or B or C, and that will cause some amount of parsing. But you will need to know where in the input String it lies, either by telling the user the usage format, or searching the string.

    Let’s say you have the user to use the following method to input the parameters:

    [program] A=2 B=3 C=A+B
    

    Since WChargin pointed out that args is space-delimted, which slipped my mind, I decided to break up each set of arguments into their own string array. For A and B I split the string by the delimiter by the character “=” as so:

    if(args[i].toUpperCase().startsWith("A"))   
    {
         resultA = args[i].split("=");  //Split by the delimter "="
         a = Double.parseDouble(resultA[1]);
    }
    

    Which for A and B will produce the array {A,2}, {B,3}. C I will split twice. First by the character “=” which will produce {C,A+B} and then split each string, which will produce { ,A,+,B}. Note that split() produces an empty string in resultC[0], so we start iterating at 1.

    We will simply check the length of args, and iterate through to find the parameters values:

    public static void main(String[] args)
    {
        double a = 0;
        double b = 0; 
        double c = 0;
        String[] resultA = null;
        String[] resultB = null;
        String[] resultC = null;
        String[] result = null;
    
        if(args.length > 0)
        {
            for(int i=0; i < args.length; i++)
            {   
                if(args[i].toUpperCase().startsWith("A"))   // Implemented startsWith() thanks to WChargin
                {
                    resultA = args[i].split("=");   //Split by the delimter "="
                    a = Double.parseDouble(resultA[1]);
                }
                else if(args[i].toUpperCase().startsWith("B"))
                {
                    resultB = args[i].split("=");
                    b = Double.parseDouble(resultB[1]);
                }
                else if(args[i].toUpperCase().startsWith("C"))
                {
                    result = args[i].split("="); //We don't need C, split all of the arguments
                    resultC = result[1].split(""); //This way we have an array of strings for each value to iterate through
                    // The only problem with split("") is that we will get an empty string at the beginning of the array
    
                    for(int j=1; j < resultC.length; j++)
                    {
                        if(resultC[j].toUpperCase().startsWith("A"))
                        {
                            if(resultC[j+1].equals("+"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = a + a;
                                    break;
                                    // Once we get out answer, break otherwise we'll get a ArrayIndexOutOfBoundsException because the program will continue iterating
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = a + b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("-"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = a - a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = a - b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("*"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = a * a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = a * b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("/"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = a / a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = a / b;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                        }
                        else if(resultC[j].toUpperCase().startsWith("B"))
                        {
                            if(resultC[j+1].equals("+"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = b + a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = b + b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("-"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = b - a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = b - b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("*"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = b * a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = b * b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                            else if(resultC[j+1].equals("/"))
                            {
                                if(resultC[j+2].toUpperCase().startsWith("A"))
                                {
                                    c = b / a;
                                    break;
                                }
                                else if(resultC[j+2].toUpperCase().startsWith("B"))
                                {
                                    c = b / b;
                                    break;
                                }
                                else
                                {
                                    System.out.println("Argument parse error");
                                }
                            }
                        }
                        else
                        {
                            System.out.println("Argument error in C");
                            System.exit(1);
                        }
                    }
                }
            }
        }
        else
        {
            System.out.println("Invalid number of arguments");
            System.exit(1);
        }
    
        System.out.printf("A: %f\nB: %f\nC: %f\n", a, b, c);
    }
    

    Please note I probably did not account for all possibilities.

    There are definitely easier ways to parse through command line arguments. I am giving you the overly long method.

    I hope this helps you!

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

Sidebar

Related Questions

This question got me thinking about bare strings. When PHP sees a string that's
Just wondering about this, is it possible to use Django with the Google Apps
This is just something I've been thinking about and was wondering if it exists,
Been thinking about this for hours now. Im building a simple slideshow application, where
Maybe I'm thinking about this the wrong way but here's the idea: Class A
I was thinking about this earlier and figured I'd ask. If I made an
I'm thinking about this question for a time: when does an ARM7(with 3 pipelines)
I've been thinking about this and I just can't think of a way to
This question got me thinking about the max_size method in vector class. It is
I'm thinking about building a login system for Ruby on Rails, much like this

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.