aspose file tools*
The moose likes Beginning Java and the fly likes Running a Program from Elements in its Title Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a Program from Elements in its Title" Watch "Running a Program from Elements in its Title" New topic
Author

Running a Program from Elements in its Title

Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Hello All,

This may be a "too simple" question for most savvy Java people but I'm just beginning so here goes. I have a programming assignment as follows:

INSTRUCTIONS:
Write a program that will read in a number from the command line and tell me if it is even or odd.
In other words, I want to type
java EvenOrOdd 28
and see
even

I have the code worked out where the user inputs a number and the program returns "even" or "odd". The technical part of the code that determines an even number or an odd number I understand already but the instructions specifically say to use part of the title of the program to determine the output.

In the instructions above, for instance, the number 28 is part of the title of the program that is run and the program uses it to determine the outcome (or output) of its calculation. Intuitively, I know that somehow the program has to take the number 28 from the title and convert it to an int variable (since all input starts out as string???), then insert that int variable into the code for the calculation.

Any help would be great. Thanks!
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
You can achieve this by examining the signature and the working of "the" main method. Somewhere in here
you will find a hint as to how to do it..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
You appear to be asking two questions.
I think you want an answer to no 2

1: Set up a Java program which calls "java EvenOrOdd 28" and runs it. For that, find the article my Michael Daconta by googling for "when Runtime.exec() won't" and it has a number of examples which show retrieval from the output stream of the other application.

2: How to create a program which takes 28 written on the command line and prints "even."
You are trying to turn the String into a number, an int. As you know, there is a wrapper class corresponding, called Integer and it has methods for doing useful things with integers, and can even parse to an int. Go through its list of methods.

You know that the command-line arguments are passed to the main method as an array of Strings, and you usually call that args?
Well you know how you get the first member of an array with its index? There is a bit in the Java Tutorials about it. So you can get your hands on the first member of "args" similarly.

By the way: It sounds pedantic, but the 28 is not actually part of the title of the program. It is an argument (or command-line argument).
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Thanks guys. That's an awesome piece of information. Just knowing that I'm dealing with a command-line argument makes all the difference in the world. I understand it a lot better now and have reworked my code to reflect this. Thanks again!
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Here's my code to that program that checks if a number is even or odd. Will y'all check to see if I'm on the right track now? Thanks!




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

Joined: Oct 05, 2008
Posts: 49
Oh by the way, I think I understand the reason we code "...main(String[] args)" at the beginning of each program - so it can take command-line arguments !!!???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Originally posted by Benjamin Chau:
. . . we code "...main(String[] args)" . . . so it can take command-line arguments !!!???


Yes.
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
OK, now that I can compile, I did so with this program (EvenOrOdd) and here's what I get as the output.


C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac EvenOrOdd.java
EvenOrOdd.java:11: illegal start of expression
if ( (Integer.parseInt(args[0])) % 2 ) == 0 );
^
EvenOrOdd.java:15: 'else' without 'if'
else
^
2 errors

For the first error, I'm thinking the way I wrote the first if statement is wrong though the logic is correct? I was trying to take the command-line argument and convert it to an integer. And for the other error - I thought that an else statement can stand alone without an if keyword (as opposed to: "else if")
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3651
    
  15
Count the number of brackets you open and then count how many you close.

And remove the ; at the end of that line
[ November 14, 2008: Message edited by: Joanne Neal ]

Joanne
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Joanne,

What you said to do was actually my original code and it didn't work. I rewrote it to reflect my original and here's the output (still 2 errors). Somethings weird...



C:\Documents and Settings\Benjamin Q. Chau\WRITTENPROGRAMS>javac EvenOrOdd.java
EvenOrOdd.java:10: illegal start of expression
if ( (Integer.parseInt(args[0])) % 2 ) == 0 )
^
EvenOrOdd.java:16: ';' expected
^
2 errors
Zaynab Ejaz
Greenhorn

Joined: Nov 14, 2008
Posts: 6
Leave a space after the if
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Let's dissect your if-statement:

As you can see, and Joanne already suggested, there is one ) too many.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Get a decent text editor. Notepad isn't much good for programming. Look for Notepad++ Notepad2 or jedit, and try them. They have nice features like bracket matching; it you put your mouse on one ([{ its opposite number changes colour so you can see whether you have them correctly matched. They also set up your indentation automatically, but you will probably have to change the default from 8 spaces to 4 spaces. You can find a lot of errors quickly with bracket matching.
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Got it! I was trying to make things clear by using brackets/parantheses to separate everything but I guess less is really better! Thanks all!

Campbell, I will heed your advice and download NotePad2...

Here's the code that worked!


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

Joined: Oct 13, 2005
Posts: 39408
    
  28
You are probably correct to keep the {}; you just need to make sure they are properly paired.

And once you get it working, see how nice and simple the code is
Benjamin Chau
Ranch Hand

Joined: Oct 05, 2008
Posts: 49
Yep, nice and simple. And you get a good feeling of accomplishment!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Running a Program from Elements in its Title