aspose file tools*
The moose likes Beginning Java and the fly likes Program to Access an Array Using a Command-Line Argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program to Access an Array Using a Command-Line Argument" Watch "Program to Access an Array Using a Command-Line Argument" New topic
Author

Program to Access an Array Using a Command-Line Argument

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Hey everyone. I have this programming assignment with instructions below followed by my code. Will y'all take a look to make sure I'm on the right track? Thanks so much!

INSTRUCTIONS:
Purpose: To learn how to build and access an array.
Write a program that will read in a number from 0 to 25 and spell out that number. The program must also report any values that are out of range.
In other words, I want to type
java Say 22
and see
twenty-two



][/CODE][edit]Add code tags. CR[/edit]

[ November 07, 2008: Message edited by: Campbell Ritchie ]

[ November 07, 2008: Message edited by: Benjamin Chau ]

[ November 07, 2008: Message edited by: Benjamin Chau ]

[ November 07, 2008: Message edited by: Benjamin Chau ]

[ November 07, 2008: Message edited by: Benjamin Chau ]
[ November 07, 2008: Message edited by: Benjamin Chau ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
Thank you for the e-mail. We have a policy of not answering questions by e-mail; everybody should be able to read the answer. And please maintain indentation with the code button; code is difficult to read without.

So what goes wrong when you try to compile that class? The error message "expected boolean found int" usually means you have used = when you are supposed to use ==. Does anything else go wrong?
And I think it would be better to print "number out of our range" than "invalid entry."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
And what does it say the purpose of the question is?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

And what is the purpose of the loop?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
I hadn't noticed the loop. Nor had I noticed there are { and } missing, and two ;s missing.
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Ok Campbell, I trimmed it down and edited it per the guidelines. And Henry, you're right - I don't need a loop after all. I wanted to limit the user to only numbers from 0 to 25. In review of the code, I don't need a loop at all, or even an if..else statement. I'll fix my code to reflect this. Is that y'all's thinking also?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Well, what happen when you compiled it? You still have to fix the many compile errors.

Henry
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Henry,

I'm still having problems compiling. It has something to do with setting my envirornmental path variable correctly. No matter what I try, I can't seem to get it right. Still working on that. Thanks for asking though...

Benjamin
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Originally posted by Benjamin Chau:

I'm still having problems compiling. It has something to do with setting my envirornmental path variable correctly. No matter what I try, I can't seem to get it right. Still working on that. Thanks for asking though...



I would recommend that you don't write another line of code until you get the build environment going. Without this environment, you could be building crap without knowing it.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

Originally posted by Benjamin Chau:
I'm still having problems compiling. It has something to do with setting my envirornmental path variable correctly.

What, EXACTLY, is the text of the compiler error message? That tells you a LOT about what you need to start looking at.

if you get something like
'javac' is not recognized as an internal or external command,
operable program or batch file.

You need to work on your PATH. if you get something like
javac: file not found: fred.java
Usage: javac <options> <source files>
use -help for a list of possible options

it's a problem with you CLASSPATH. Other error can point to problems with your code. There is no way for anyone to help without you telling us what the real problem is.

Even once you get your environment working, I think you need to read the problem again. It specifically says "Purpose: To learn how to build and access an array." I don't see you building an array, although one could argue you are accessing the args[] array. I think if you continue with this algorithm, your instructor will probably mark you down.

But please heed Henry's advice. Get your environment working FIRST, and only then worry about your code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Fred,

It's the first one:

'javac' is not recognized as an internal or external command,
operable program or batch file.

Others have tried to help me out and I've done as their advice says (change the Environmental Path Variable) but I still can't get it right. I'm running XP Media Edition and here's exactly what I'm doing:

1) I pull up Control Panel then double-click the System icon.

2) I click on the Advanced tab, then the box that reads: Environment Variables.

3) I click the button New under the System variables space and when a box pops up I type in Variable name and the Variable value, using JAVA as the name and the following directory for the value (since that's where the Java software is located on my computer). Here's the exact directory I type in for the Variable value:

C:\Program Files\Java\jre1.5.0_06\bin

4) I repeat the exact same steps as above (3)) for the space that reads: User variables for my name.

5) I open my Command Prompt window and try to compile a program, javac HelloWorld, for instance. Then I get that error message again.

'javac' is not recognized as an internal or external command,
operable program or batch file.

Now when I open up the Command Window and look at the default Path Variable, I see the same directory as always, unchanged, despite all the steps I've taken as described above. Here's the default Path Variable that I see in Command Prompt that never seems to go away:

C:\Documents and Settings\my name>


Any suggestions would be greatly appreciated...

Benjamin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38784
    
  23
I have answered on your other thread. You may need to reinstall Java.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

have you checked to be sure there really IS a "javac.exe" in that directory?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Here's the exact directory I type in for the Variable value:

C:\Program Files\Java\jre1.5.0_06\bin


A "JRE", aka Java Runtime Environment, is used for running Java only. It doesn't include a Java compiler.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program to Access an Array Using a Command-Line Argument