Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default [SOLVED] Errors with project: specify array size by the value in first command-line a

    I need assistance with a homework assignment. The project was to rewrite an existing textbook example so that the array size is specified by the value in the first command-line arg. Here is the code. It giving errors for lines 21, 25, 28, 29, 32, but I can't figure out why. Can someone please help me debug the cause of the errors?


    Java Code:
    import java.util.Scanner; // program uses class Scanner
    
    public class InitArrayNew
    {
       public static void main( String args[] )
       {
          int array[]; // declare array named array
          int inputNumber = 0; // number value entered
          array = new int[ inputNumber ]; // create the space for array
    
          // prompt for user to input a number for the array size
             System.out.println( "Please enter a number for the array size: " );
             int num = input.nextInt();
    
          // validate the input
             if (inputNumber > 0)
                 array[length] = array[num];
    	 	 }
    		 else
    		 {
    			 array[length] = array[10];
    	  	 }
          System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
    
          // output each array element's value
          for ( int counter = 0; counter < array.length; counter++ )
             System.out.printf( "%5d%8d\n", counter, array[ counter ] );
    
       } // end main
    } // end class InitArrayNew

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

    Default observations...

    Some observations about your code:
    • Don't call your array "array". Call it something else: "myArray" or "myHeadAche" or... etc.
    • You are working with arrays (which are different from arraylists). When you create an array and define it's size, it has to stay that size. For example, if you create/define your array with 5 elements and you try to access the 6th element, your program will send you a outOfBounds nastygram.
    • You can't create an array with zero elements (black hole effect: program implodes at compile time).
    • Therefore, you have to define the size of your array after getting the user's array size.
    • You can't output the array's elements values if you haven't input the elements. You need a "for" loop to input the array elements.

    Here's a good link about arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

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

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

    Default

    Thanks for the input. Just to clarify, I didn't name the array "array". We were given a pre-existing template that already had the array named "array" with an array size of 10, and my homework was to rewrite the existing file to make the array size be specified by the first command-line argument but if no command-line arg is supplied, to leave 10 as the default size. Just an FYI, the original template I was given is below.

    Java Code:
    public class InitArray
    {
       public static void main( String args[] )
       {
          int array[]; // declare array named array
    
          array = new int[ 10 ]; // create the space for array
    
          System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
    
          // output each array element's value
          for ( int counter = 0; counter < array.length; counter++ )
             System.out.printf( "%5d%8d\n", counter, array[ counter ] );
       } // end main
    } // end class InitArray

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    regardless of the name of your array variable, your code is all wrong:

    1) you never use the command line arguments (args) passed to your main method. That's where the number should be passed.
    2) You're using an object, input, that's never declared. what is this animal? where did it come from? how is it supposed to get input?
    2) You're using a variable, length, that is never declared. Again, what is this, where did it come from?

    You can't just make up variables and methods and hope that they'll work. Programming doesn't work that way.

    Your solution is to use the args passed to your main method, check to see that an arg was in fact passed (that the array length is > 0), convert the passed String to an int (using a method from the Integer class), and then creating your array with this result.
    Last edited by Fubarable; 01-03-2009 at 04:59 AM.

  5. #5
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable
    Your solution is to use the args passed to your main method, check to see that an arg was in fact passed (that the array length is > 0), convert the passed String to an int (using a method from the Integer class), and then creating your array with this result.
    @jhrh95
    It seems you are very young and new to Java. So maybe you'll find it difficult to understand Fubarable's answer. The coded-one is:
    Java Code:
    int myLength = Integer.parseInt(args[0]);
    If you can't understand that, feel free to ask questions

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

    Default

    Thanks for the help, and I do have questions. The homework assignment was to rewrite the original code so that the array size is specified by the first command-line arg, but if none was given to default the size to 10. So, I was originally prompting for user input to specify the size of the array they wanted, but I think that I just was confused on what I needed to do... and I'm still a bit unclear. I started all over, thinking it would help, but I still don't entirely get it. The output of the original file is below as well as my new code. Can someone please help steer me in the right direction?

    Original file output:

    Index Value
    0 0
    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0

    My new code:
    Java Code:
    public class InitArrayNew2
    {
       public static void main( String args[] )
       {
    
    		// check number of command-line arguments
    		if (args.length !=10 )
    		{
    			I'm not sure what to do here
    		}
    		else
    		{
    			// get array size from first command-line argument
    			int arrayLength = Integer.parseInt( args[ 0 ]);
    			int array[] = new int[ arrayLength ]; // create the space for array
    
    			// get initial value and increment from command-line arguments
    			int initialValue = Integer.parseInt( args[ 1 ]);
    			int increment = Integer.parseInt( args[ 2 ] );
    		}
    
    	
    		System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
    
    		// output each array element's value
    		for ( int counter = 0; counter < array.length; counter++ )
    		System.out.printf( "%5d%8d\n", counter, array[ counter ] );
    
       } // end main
    } // end class InitArrayNew2
    Last edited by jhrh95; 01-04-2009 at 07:51 PM.

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

    Default

    First you should check to see if the user specified any arguments...
    Java Code:
    if (args.length == 0)
      {
        set default to 10;
       }
    else
       {
        get the amount that the user specified
       }
    Now... you have to output each array element value... how can you do that if you haven't put any values in the array? As you're example shows, it contains nothing (printing zeros).
    First read the link that I put in my previous post. Once you understand how to put values into an array, with a "for" loop, populate the array with values (any values). Then, you can print out the array elements like you currently are.

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

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

    Default

    For my homework, I don't need to do anything with the element values; they can stay and print out as zero. I just need to make the array size be what the first command line argument is or default it to 0. I'll review the array tutorial again.

  9. #9
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    If you don't need the array elements, why:
    Java Code:
    if (args.length !=10 )
    I can't understand

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

    Default

    There are 10 items in the Index (0 - 9), but the values (elements) are all 0 and can remain zero is all I was trying to communicate.

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

    Default

    OK, my revised code is below but I'm still getting an error on line 21: (InitArrayNew2.java:21: cannot find symbol
    symbol : variable length
    location: classInitArrayNew2
    a[length] = a[10];

    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 )
    		{
    			a[length] = a[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

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

    Default OK...

    If you don't need to print any array elements, then the output in your first post is OK, assuming that the user didn't specify any command line arguments. All you have to do is find out if the user specified any arguments and my previous post has the solution for that.

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

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

    Default Whoa...

    You just did a NO-NO !!! Why in the world did you crosspost ???
    New To Java - Need help with array project
    You're not supposed to do that. It irritates the hell out of people trying to help you (like me). It's also a great way to not get any help.

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

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

    Default

    Java Code:
    a[length] = a[10];
    No, that is not any good. Just assign 10 to the aLength variable.
    With that change, your code work OK.

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

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

    Default

    I wasn't trying to double post. I was trying to edit my original and change the name/verbiage. Did I mess something up?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posting is when you post the same question in another forum, and it can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

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

    Default

    One last question related to your comment above: Can you direct me to where in the FAQ/Bulletin FAQ is references cross-posts? I've not seen a reference anywhere.

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

    Default

    Thanks for the info. Sorry. I thought it was completely different groups of people and wasn't trying to give anyone any headaches.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No problem. Doing it once or twice is OK, but many who do it multiple times get ignored.

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

    Default

    CJASLMAN, can you please clarify the above item: Just assign 10 to the aLength variable. With that change, your code work OK.

    Java Code:
    a[length] = a[10];
    I don't have a separate aLength variable.

Page 1 of 2 12 LastLast

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
  •