-6

I don't have to much experience in java and I'm currently taking a tutorial in java. But I have a problem with a thing that I already seen a couple of times in this tutorials. Here is my code:

public class GuessingGame {
    public static void main(String args[]) {
        int randomNum = 0;
        int argument;
        if (args.length == 0 || args[0].compareTo("help") == 0) {
            System.out.println("Usage: GuessingGame [argument]");
            System.out.println();
            System.out.println("help print this help message");
            System.out.println("Enter 1-5 as your guess");
        } else {
            randomNum = ((int)(Math.random() * 5) + 1);
            argument = Integer.parseInt(args[0]);
            if (argument < 1 || argument > 5) {
                System.out.println("Invalid argument !!!");
            } else {
                if (argument == randomNum) {
                    System.out.println("Great Guess - You got it right !!!");
                } else {
                    System.out.println("Sorry the number was: " + randomNum + ". Try again !!!");
                }
            }
        }
    }
}

The tutorial that I take it is little bit old so it uses jEdit and command prompt to compile this program and I use IntelliJ. The problem is that when I run the program it just print those lines and I don't understand how to insert a number in order to make this program useable.

George Zoiade
  • 73
  • 1
  • 3
  • 11

1 Answers1

0

The String args[] parameter of your main method is an array of Strings which indexes can be filled when you're executing your program from the command line.

Here is how to do it in from your command line :

javac GuessingGame.java // This will compile your Java code file
java GuessingGame one two three // Here, we execute the bytecode file created from compilation

The value of String[] args here is the following :

["one", "two", "three"]

MORE INFO

PS : I do not know any method to have a filled String[] args from an IDE. It is empty by default.

Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Thx. I managed to understand the whole thing. My program can't pass an argument it just compile the program without any argument in order to execute something. – George Zoiade Oct 19 '15 at 21:17
  • 2
    `PS : I do not know any method to have a filled String[] args from an IDE. It is empty by default.` -- yes it is empty by default, but each IDE has well-defined ways of running a program with specified args. For example Eclipse has a Run Configurations wizard that lets you set the program command arguments. – Hovercraft Full Of Eels Oct 19 '15 at 21:37