• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Head First Java Chapter 11 MiniMusicCmdLine

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.

What's up? Any help would be appreciated

 
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suspect that you are not setting the command line args in Eclipse correctly. Can you take a snapshot of that step and post it?
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eclipse is telling you exactly what's wrong: you are creating variables but not using them.
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
William Oliver-Diaz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go.
Screen-Shot-2020-06-06-at-20.28.57.png
[Thumbnail for Screen-Shot-2020-06-06-at-20.28.57.png]
 
Rancher
Posts: 179
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two separate issues here.

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.

In order to fix this, you'll need to set up a run configuration and pass in your instrument and note in there.
I found a great guide on this with screenshots: https://www.cs.colostate.edu/helpdocs/cmd.pdf
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wasn't the screenshot I was referring to. I meant the Eclipse Runtime Config window where you enter the command line arguments, not your program.
 
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zachary Griggs wrote:. . . . 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. . . .

And that part of the code (=line 14) is different from what it says in HFJ (page 346 ).
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Yes, but those are just warnings. It still should have run with the 5, 5 parameters.


You're right, thanks.

William Oliver-Diaz: if you right-click the class, select Run As, select Run Configurations, then click the arguments tab, what do you have in the Program Arguments box?
 
William Oliver-Diaz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback so far - is this what you're asking for?

Screen-Shot-2020-06-07-at-12.33.18.png
[Thumbnail for Screen-Shot-2020-06-07-at-12.33.18.png]
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You want “program arguments”.
 
William Oliver-Diaz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'm not sure what I should do. Do I click 'variables' and add stuff?
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, You write the command line arguments in the box under “program arguments”. Or you copy the entire XXX.java file to a different directory and use the command line tools javac and java.
 
William Oliver-Diaz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what format? I've tried a few different things and it doesn't work. For example do I type 10, 10 or "10", "10" or instrument=10.. etc
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the command line.
 
William Oliver-Diaz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 69493
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic