Results 1 to 5 of 5
  1. #1
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default [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!!

  2. #2
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    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.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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?
    Super big hint: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    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.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 03:45 PM
  2. Printing command line arguments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:27 AM
  3. Replies: 1
    Last Post: 11-07-2007, 02:17 PM
  4. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM
  5. Java Command Line Arguments In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-19-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •