Results 1 to 5 of 5
- 11-16-2008, 06:19 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
[SOLVED] Command Line Arguments and ParseInt
Hi. This is what Im working on.
1. Write a program Average.java that just prints the strings that it is given at the command line, one per line. If nothing
is given at the command line, print "No arguments".
2. Modify your program so that it assumes the arguments given at the command line are integers. If there are no
arguments, print a message. If there is at least one argument, compute and print the average of the arguments. Note
that you will need to use the parseInt method of the Integer class to extract integer values from the strings that are
passed in. If any non-integer values are passed in, your program will produce an error, which is unavoidable at this
point.
Can anyone help explain how to do it and how to convert String to integer using parseint? If possilbe, provide an example? I havent learned
Here's what I have so far.
public class Command
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
//How to print "No arguments" if nothing is entered? Do I use if else and .equals method? How do I use parseInt?
}
}
This is all I know about command line arguments:
public class NameTag
{
public static void main (String[] args)
{
System.out.println ();
System.out.println (" " + args[0]);
System.out.println ("My name is " + args[1]);
}
}
THANKS FOR THE HELP!!
- 11-16-2008, 06:52 AM #2
Member
- Join Date
- Jul 2008
- Posts
- 68
- Rep Power
- 0
First you need to figure out how to tell if any arguments were actually passed on the command line. Read the String Array API to see if there are any methods that will tell you how many elements are in your array "args"
Next read the Integer API, more specifically the parseInt method. With those two pieces of knowledge you should be able to write your program very easily.
I would google javadoc String Array and javadoc Integer. Documentation is the most powerful tool a developer can leverage.
- 11-16-2008, 04:58 PM #3
Step by step...
Some questions to get you started in the right way:
- Do you know what "String[] args" is? (HINT: it's a type of container)
- What kind of objects does this particular container store?
Once you have answered the above, the you then can look at the methods that are available (for this type of container) to find out things about the conntainer like:
- Is the container empty?
- If not, how many objects does the container have?
- How do I get the objects out of the container?
These answers would be answered by consulting the following spec:
Java Platform SE 6
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-16-2008, 05:23 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Thanks for the help.
I recently just learned arrays and using them in multiple classes. Not much was explained about the command line argumens except the last program posted. Thanks for the help again.
- 11-16-2008, 09:45 PM #5
cmd line args...
The command line arguments is nothing more than an array of strings. The methods that apply to it are here:
Array (Java Platform SE 6)
In my last post, the link to the array nuts&bolts give examples on how to access arrays and other neat stuff (like length of an array).
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Command Line Arguments
By Nakira in forum NetBeansReplies: 10Last Post: 02-04-2010, 03:45 PM -
Printing command line arguments
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:27 AM -
how to enable jcreator to accept command line arguments?
By Amateur1980 in forum JCreatorReplies: 1Last Post: 11-07-2007, 02:17 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM -
Java Command Line Arguments In Eclipse IDE
By JavaForums in forum EclipseReplies: 0Last Post: 05-19-2007, 09:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks