0

So I always use Eclipse to run my java stuff, I have no clue how to use command prompts. I have an assessment that will be graded by a bot where 2 string parameters will get passed into a function which returns a boolean value.

The bot is going to use a command like "java main.java xyz zyx" to open the file (assuming xyz and zyx are the strings).

So my question is, to catch those 2 strings, do I have to use 2 variables to catch the 2 string. For example: string1 = Scanner.nextln(); // This will catch "xyz" into string1?? string2 = Scanner.nextln(); // This will catch "zyx"??

Or does string 1 catch both "xyz zyx" and I have to use a loop to separate them into 2 strings? Thanks in advance :)

1
  • They go in your main's String[] args. Commented Mar 14, 2020 at 4:20

2 Answers 2

1

Your Main method can be used to catch arguments passed from command line.

public static void main(String[] args) {
        System.out.println(args);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok so if they enter "java main.java xyz zyx" Does the space in between them make it into 2 seperate arguments or is it only one? So can I do for example string1 = args[0]; I am confused about how to capture it into a variable
It's only one. It's a string array and you can access array with index. You may try it out by calling the function yourselves
0
public static void main(String[] args) {
    ...
}

Is your program written like this? As in the signature of the main function. The signature holds them as strings in an array. Then you have to process those strings.

2 Comments

Yeah thats my main
If the argument is: java program.java A B C. It is space delimited so there 3 arguments in total. You will have an array of [A, B, C] as strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.