I've entered the code correctly and have passed arguments. But Eclipse just gives a warning that the local int variables instrument and note in the main haven't been used and my output is just "Don't forget the instrument and note args".
It also warns that the value of the local variable event is not used.
First, the IDE warnings. What's happening here is, you declare two variables (instrument and note) but then you don't use them anywhere. Instead, you pass in the values 5, and 5. Eclipse scans for variables that never get used, and it gives you a warning since unused variables is a clear sign that you did something wrong. Warnings do not stop your program from running; they are just that. A warning that you very possibly did something wrong. With experience, you will learn which warnings should be fixed and which can be ignored. It's never a bad idea to look at them and determine for yourself if it's an issue or not (in here, it is an issue)
Second, the message coming up when you run the program. This is likely due to not setting up a run configuration. When you right click -> run on the file, it runs the java program with no arguments. Line 9 makes it so you need to have at least two arguments.
Campbell Ritchie wrote:Or you copy the entire XXX.java file to a different directory and use the command line tools javac and java.
Great thanks guys I've got it to work in the way you said now. But how do I do this? Would this make it so that I can just type the arguments into the program directly rather than in run configurations?
posted 1 month ago
On a Windows® box, I think you would open Windows Explorer, R‑drag the XXX.java file, and when you release the mouse click the copy option. On a Linux box,The third and fourth lines are the same in Windows®. You can simplify lines 3‑4 by omitting the package declaration.
posted 1 month ago
William Oliver-Diaz wrote:. . . type the arguments into the program directly . . .
Like this, maybe?I suggest you change the class name because you are no longer using the command line.
William Oliver-Diaz wrote:Great thanks guys I've got it to work in the way you said now. But how do I do this? Would this make it so that I can just type the arguments into the program directly rather than in run configurations?
What I would do is this: instead of the code which uses data from the command line:
just use hard-coded data:
You could repeat this code several times with different values to see what would happen differently, too. (You'd have to adjust the repetitions so as to only declare the variables once.)
It's a tiny ad only because the water is so cold.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop