Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Tiny problem with arguments.

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Tiny problem with arguments.

    Hello everyone,

    I got a problem with program that should take arguments and use them, but it's not using them.

    Code:
    class Hr10NewRoot {
        public static void main(String[] args) {
            //Hour ten, example one.
            int number = 100;
            if (args.length > 0) {
                number = Integer.parseInt(args[0]);
            }
            System.out.println("The square root of "
                    + number
                    + " is "
                    + Math.sqrt(number) );
        }
    }

    Output:

    run:
    The square root of 100 is 10.0
    BUILD SUCCESSFUL (total time: 0 seconds)

    Argument: 169

    Expected output: The square root of 169 is 13.0

    IDE: Netbeans(If it helps in anyway)

    This code is from a book, all steps where followed so it should work well.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Tiny problem with arguments.

    How are you passing the argument into the program?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Tiny problem with arguments.

           if (args.length > 0) {
                number = Integer.parseInt(args[0]);
            }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,342
    My Mood
    Hungover
    Thanks
    143
    Thanked 625 Times in 536 Posts

    Default Re: Tiny problem with arguments.

    ...how are you passing the argument into the program?

    Read this: Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Tiny problem with arguments.

    It looks to me as though your arguments are never greater than 0 so number always stays 100.

    If you are using the Eclipse IDE, see - http://www.javaprogrammingforums.com...s-eclipse.html

    You can test with:

    public class Hr10NewRoot {
     
        public static void main(String[] args) {
     
        	if (args.length < 1) {
        		System.out.println("There are no arguments!");
            }else{
            	System.out.println("Argument is " + args[0]);
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Tiny problem with arguments.

    I use Netbeans.
    Run - Set Project Configurations - Customize - Arguments field.

    Anyone know why it isn't working?
    Last edited by Melawe; June 16th, 2011 at 04:38 PM.

Similar Threads

  1. Problem with inheritence? arguments not applicable
    By rbk in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2011, 09:37 AM
  2. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 10:14 PM
  3. [SOLVED] generic arguments, binary tree
    By vendetta in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2010, 06:40 AM
  4. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 03:04 PM
  5. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM