Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-02-2009, 10:09 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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?


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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-02-2009, 10:41 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-03-2009, 04:50 AM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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.

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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-03-2009, 04:56 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-03-2009, 11:27 AM
Member
 
Join Date: Dec 2008
Location: Italy
Posts: 79
Rep Power: 0
raffaele181188 is on a distinguished road
Default
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:
Code:
int myLength = Integer.parseInt(args[0]);
If you can't understand that, feel free to ask questions
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-04-2009, 07:28 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-04-2009, 08:19 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default
First you should check to see if the user specified any arguments...
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-04-2009, 09:06 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-04-2009, 09:13 PM
Member
 
Join Date: Dec 2008
Location: Italy
Posts: 79
Rep Power: 0
raffaele181188 is on a distinguished road
Default
If you don't need the array elements, why:
Code:
if (args.length !=10 )
I can't understand
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-04-2009, 09:25 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-04-2009, 09:29 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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];

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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-04-2009, 09:31 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-04-2009, 09:41 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-04-2009, 09:45 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-04-2009, 09:49 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-04-2009, 10:03 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-04-2009, 10:04 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 01-04-2009, 10:05 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
Default
Thanks for the info. Sorry. I thought it was completely different groups of people and wasn't trying to give anyone any headaches.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 01-04-2009, 10:07 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
Default
No problem. Doing it once or twice is OK, but many who do it multiple times get ignored.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 01-04-2009, 10:25 PM
Member
 
Join Date: Jan 2009
Posts: 19
Rep Power: 0
jhrh95 is on a distinguished road
Default
CJASLMAN, can you please clarify the above item: Just assign 10 to the aLength variable. With that change, your code work OK.

Code:
a[length] = a[10];
I don't have a separate aLength variable.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Arguments Nakira NetBeans 4 01-10-2009 01:50 AM
JDBC Driver errors in command line but not NetBeans MeathUltra Database 7 12-08-2008 06:20 PM
Java command line agouzoul New To Java 2 04-02-2008 12:12 PM
Exporting from the command line o1121 Eclipse 1 08-09-2007 07:29 PM
Unable to execute command line command in java LordSM New To Java 1 08-08-2007 12:23 AM


All times are GMT +2. The time now is 03:17 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org