0

I have to sort a buch of integers from a text file. Is it possible to pass the numbers from the file as (String[] args) to my main method?

Cheers

1 Answer 1

4

next to the Run button (the green arrow) there's a downwards arrow for selecting the run configuration.

click it, go to run configuration. there you can add a new setup for a java program, and in the tabs you can find "Arguments"

EDIT: to read the text file:

put the path inside the run configuration arguments

now in the public static void main(String[] args) you will want to read the text file. you can read the first line, and then use args = line.split(" ")

this will override the String[] args array. remember to remove this if you export to jar, unless you will use the file path as the argument

the code for reading the file is something like this:

Scanner sc = new Scanner(new File(args[0]));
args = sc.nextLine().split(" ");
Sign up to request clarification or add additional context in comments.

Comments

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.