0

I am very new to Java and am trying to write an algorithm to take in three numbers and sort them as a way of practicing algorithm writing. Below is what I have.

package com.company;

public class Sort {
    public static void main(String[] args) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int c = Integer.parseInt(args[2]);
        int min, med, max;
    if(Math.max(a, b) == a){
        if(Math.max(a, c) == a){
            max=a;
            if(Math.max(b,c)==b){
                med=b;
                min=c;
            }else{
                med=c;
                min=b;
            }
        }else{
            max=c;
            med=a;
            min=b;
        }
    }else {
        if(Math.max(b,c)==b) {
            max = b;
            if(Math.max(a,c)==a) {
                med = a;
                min = c;
            }else {
                med = c;
                min = a;
            }
        }else{
            max=c;
            med=b;
            min=a;
            }
        }
    }
    }

When I attempt to call this file from the terminal like below I get the following error. What is causing the invalid flag error?

javac Sort.java 1 2 3
error: invalid flag: 1
Usage: javac <options> <source files>
use --help for a list of possible options
4
  • what line is the traceback referring to. Commented Jun 6, 2020 at 4:35
  • 1
    Command-line input to your program belongs on the java command, not the javac command. Commented Jun 6, 2020 at 4:38
  • if you are trying to generate the java file class then use javac initially, but yes, for command-line args you need to use java Sort without the .java extension. Commented Jun 6, 2020 at 4:41
  • javac Sort.java followed by java Sort 1 2 3 Commented Jun 6, 2020 at 4:42

3 Answers 3

1

If you want to compile your code, you do ist with javac Sort.java in the console. As you can see, without parameter. In you want run the code, you do it with java Sort 1 2 3. Here you need to use the parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

When running it I actually had to do "java Sort.java 1 2 3", not sure if that's what you meant or not but it appears to be working now.
0

Command-line arguments are passed when you're invoking the program. In Java, you compile the code with javac command and you invoke the program with java command. So, when you pass arguments when invoking the program, those arguments are passed to the main function.

The error you're seeing is from javac. javac is also a program that expects a java file to compile and some optional flags (for eg, to suppress warnings, etc). javac is not able to recognize 1 as a flag. just say javac Sort.java and it will compile your code. After that, run your program with java command, which itself is also a program that takes a class with optional arguments that are passed to the class while invoking it. So, call your program like java Sort 123

Comments

0

First compile your code as you compile any java code :

javac Sort.java

Now pass the command line arguments during run time :

java Sort 1 2 3

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.