Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default My head hurts...

    I didn't understand your comment... not sure if you understood my last comment. I'll try again...
    forget the following... get rid of it... it doesn't do anything... it doesn't compile:
    Java Code:
    a[length] = a[10];
    Just like you assigned the user's input to aLength:
    Java Code:
    int aLength = Integer.parseInt( args[ 0 ]);
    ..you need to do the same but assigning 10 to aLength when the user didn't specify any input.

    Now did I make myseld a more clearer?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  2. #22
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    It compiles with no errors but when executing does nothing. :(

    Java Code:
    import java.util.Scanner; // program uses class Scanner
    
    public class InitArrayNew2
    {
       public static void main( String args[] )
       {
    
    		// create Scanner to obtain input from command window
    		Scanner input = new Scanner( System.in);
    
    		// declarations
    		int size = input.nextInt();
    		int a[] = new int [size];
        	int inputNumber = 0; // number value entered
    
    		// check number of command-line arguments
    		if (args.length == 0 )
    		{
    			int aLength = Integer.parseInt( args[ 10 ]);
    		}
    		else
    		{
    			// prompt for user to input a number for the array size
    			System.out.println( "Please enter a number for the array size: " );
    	//		array = new int[ input.nextInt() ]; // create the space for array and input number
    
    			// get array size from first command-line argument
    			int aLength = Integer.parseInt( args[ 0 ]);
    			// int a[] = new int[ arrayLength ]; // create the space for array
    		}
    
    		for(int i = 0; i <a.length; i++)
    		{
    			a[i] = input.nextInt();
    		}
    
    		System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
    
    		// output each array element's value
    		for ( int counter = 0; counter < a.length; counter++ )
    		System.out.printf( "%5d%8d\n", counter, a[ counter ] );
    
       } // end main
    } // end class InitArrayNew2

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

    Default I give up...

    I don't know what part of "assign 10 to aLength" you don't understand, but here's what I mean:
    Java Code:
    if (args.length == 0 )
    		{
    	               [B][COLOR="Blue"]int aLength = 10;[/COLOR][/B]
    		}
    		else ...
    Since the original requirement is:
    the array size is specified by the value in the first command-line arg
    please get rid the following code:
    Java Code:
    System.out.println( "Please enter a number for the array size: " )
    array = new int[ input.nextInt() ];
    Place this code:
    Java Code:
    int a[] = new int[ arrayLength ];
    after the "else" condition (after the last "else" bracket)

    It appears that your also trying to input values in to the array. I'll let you tackle that.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 03:45 PM
  2. Replies: 7
    Last Post: 12-08-2008, 06:20 PM
  3. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  4. Exporting from the command line
    By o1121 in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:29 PM
  5. 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

Posting Permissions

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