Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,307 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 782 people online right now. Registration is fast and FREE... Join Now!




Specify array size by first cmd-line arg

 
Reply to this topicStart new topic

Specify array size by first cmd-line arg

jhrh95
2 Jan, 2009 - 01:00 PM
Post #1

New D.I.C Head
*

Joined: 1 Jan, 2009
Posts: 3

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. I'm new to Java and receiving 9 errors.

Here is the code. A screenshot of the errors is attached.


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




Attached File(s)
Attached File  Doc1.doc ( 102k ) Number of downloads: 4

User is offlineProfile CardPM
+Quote Post


mostyfriedman
RE: Specify Array Size By First Cmd-line Arg
2 Jan, 2009 - 01:14 PM
Post #2

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 1,771



Thanked: 206 times
Dream Kudos: 500
My Contributions
ouch, a lot of mess you got there..ok lets start by the array creation
CODE

Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int [] a = new int [size];

over here we created an array called a with length = size which you specify, i really dont understand all those steps that you did..
now lets say you want to input values in your array so
CODE

for(int i = 0; i <a.length; i++)
{
     a[i] = sc.nextInt();
}

try to understand these codes and tell me if you have any problems smile.gif
User is online!Profile CardPM
+Quote Post

BetaWar
RE: Specify Array Size By First Cmd-line Arg
2 Jan, 2009 - 01:15 PM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 3,813



Thanked: 203 times
Dream Kudos: 1325
My Contributions
There are a few errors you are facing.

First, you aren't actually doing anything for user input. Second, your setting the array alues is... well filled with bugs. Third, you never set input as a Scanner, so that causes an error, and on and on.

Here is some sample working code (note it may not do exactly what you are looking for, but I believe it should be able to give you a lead in the right direction.

CODE
import java.util.Scanner; // program uses class Scanner

public class Runner
{
   public static void main(String args[]){
      int array[];
      Scanner input = new Scanner(System.in);
      System.out.println( "Please enter a number for the array size: " );
      array = new int[input.nextInt()];
      System.out.println("Please input "+array.length+" digits.");
      for(int i=0; i<array.length; i++){
          array[i] = (int)input.nextInt();
      }
      echo(array);
   }
   public static void echo(int array[]){
       System.out.print("Your array looks like so: \n[");
       for(int i=0; i<array.length; i++){
          System.out.print(array[i]);
          if(i != array.length-1){
              System.out.print(", ");
          }
       }
       System.out.println("]");
   }
}


HTH
User is offlineProfile CardPM
+Quote Post

jhrh95
RE: Specify Array Size By First Cmd-line Arg
2 Jan, 2009 - 08:06 PM
Post #4

New D.I.C Head
*

Joined: 1 Jan, 2009
Posts: 3

Just to clarify, 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 (that's the reason I used the If and Else statements on lines 26-32)) Just an FYI, the original template I was given is below as well as my revised code. I've been continuing to work on this all day, but I'm still getting errors (lines 28 and 32).

Original code:

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


My revised code:
CODE

import java.util.Scanner; // program uses class Scanner

public class InitArrayNew
{
   public static void main( String args[] )
   {

      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in);

      // declarations
      int array[]; // declare array named array

      // 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

      int num = input.nextInt();
      int inputNumber = 0; // number value entered
    //  array = new int[ inputNumber ]; // create the space for array and


      // validate the input
      if (inputNumber > 0)
      {
      array[num] = array[length];
}
      else
      {
      array[10] = array[length];
      }
      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

User is offlineProfile CardPM
+Quote Post

mostyfriedman
RE: Specify Array Size By First Cmd-line Arg
2 Jan, 2009 - 08:17 PM
Post #5

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 1,771



Thanked: 206 times
Dream Kudos: 500
My Contributions
CODE

array[num] = array[length];
array[10] = array[length];

its because you didnt declare and initialize length first
User is online!Profile CardPM
+Quote Post

BigAnt
RE: Specify Array Size By First Cmd-line Arg
3 Jan, 2009 - 03:21 PM
Post #6

May Your Swords Stay Sharp
Group Icon

Joined: 16 Aug, 2008
Posts: 2,384



Thanked: 95 times
Dream Kudos: 75
My Contributions
QUOTE
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


Do you mean a command line argument specified when the program is run? If so you could do something like:

java

if(args.length == 0){
arrays = new int[10];
} else {
arrays = new int[Integer.parseInt(args[0])];
}


Now this is assuming that the user always enter an integer as the first argument, if not you will get an exception and have to handle that as appropriate.
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Specify Array Size By First Cmd-line Arg
3 Jan, 2009 - 04:11 PM
Post #7

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 3,813



Thanked: 203 times
Dream Kudos: 1325
My Contributions
I think this should work for what you are trying:
CODE
import java.util.Scanner;

public class Runner{
   public static void main(String args[]){
      Scanner input = new Scanner(System.in);
      int array[];
      System.out.println( "Please enter a number for the array size: " );
      int length = input.nextInt();
      if(length <= 0){
          array = new int[10];
      }
      else{
          array = new int[length];
      }
      System.out.println("Input "+array.length+" digits:");
      for(int i=0; i<array.length; i++){
          array[i] = (int)input.nextInt();
      }
      echo(array);
   }
   public static void echo(int array[]){
       System.out.print("Your array looks like so: \n[");
       for(int i=0; i<array.length; i++){
          System.out.print(array[i]);
          if(i != array.length-1){
              System.out.print(", ");
          }
       }
       System.out.println("]");
   }
}


Condensed down to what your code is currently doing looks like so:
CODE
import java.util.Scanner;

public class Runner{
   public static void main(String args[]){
      Scanner input = new Scanner(System.in);
      int array[];
      System.out.println( "Please enter a number for the array size: " );
      int length = input.nextInt();
      if(length <= 0){
          array = new int[10];
      }
      else{
          array = new int[length];
      }
   }
}


Hope that helps.
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Specify Array Size By First Cmd-line Arg
3 Jan, 2009 - 04:19 PM
Post #8

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
Command-line arguments usually referr to the arguments passed to the String args[] variable. Bigants proposition is likely what you're looking for.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Specify Array Size By First Cmd-line Arg
3 Jan, 2009 - 05:16 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(BetaWar @ 3 Jan, 2009 - 04:11 PM) *

I think this should work for what you are trying:
CODE
import java.util.Scanner;

public class Runner{
   public static void main(String args[]){
      Scanner input = new Scanner(System.in);
      int array[];
      System.out.println( "Please enter a number for the array size: " );
      int length = input.nextInt();
      if(length <= 0){
          array = new int[10];
      }
      else{
          array = new int[length];
      }
      System.out.println("Input "+array.length+" digits:");
      for(int i=0; i<array.length; i++){
          array[i] = (int)input.nextInt();
      }
      echo(array);
   }
   public static void echo(int array[]){
       System.out.print("Your array looks like so: \n[");
       for(int i=0; i<array.length; i++){
          System.out.print(array[i]);
          if(i != array.length-1){
              System.out.print(", ");
          }
       }
       System.out.println("]");
   }
}


Condensed down to what your code is currently doing looks like so:
CODE
import java.util.Scanner;

public class Runner{
   public static void main(String args[]){
      Scanner input = new Scanner(System.in);
      int array[];
      System.out.println( "Please enter a number for the array size: " );
      int length = input.nextInt();
      if(length <= 0){
          array = new int[10];
      }
      else{
          array = new int[length];
      }
   }
}


Hope that helps.


Do you want the array length passed as argument (as the topic Title suggests) or not ?
If it is the case:
int array[] = new int[Integer.parseInt(arg[0]));
else
change the Post name

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:53PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month