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

Thread: My program compiles and runs, but doesn't display anything!

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My program compiles and runs, but doesn't display anything!

    Hey all, first time poster here, so I'll try to abide by the rules =)

    Anyway, I have a homework problem and I've got it running, but not really running. My brain is pretty fried so I'm guessing it's something silly, but if you guys could give me a hand here or point me in the right direction, it'd be very helpful. The question is asking us to rewrite an earlier example so that the size of the array is defined in the first command line argument. If there is no command argument is given, then just use 10 as the default size.

    The original code is this:

    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

    which gives me an output of:

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

    So now my code is as follows:

    import java.util.Scanner; 
     
    public class InitArray
    {
       public static void main( String args[] )
       {
     
    	  Scanner input = new Scanner( System.in);
     
    	  int array[]; 
     
    	  System.out.print("Please enter a number for the array size: ");
    	  array = new int[ input.nextInt() ];
     
    	  int num = input.nextInt();
    	  int inputNumber = 0;
     
    	  if(args.length == 0)
    	  {
    	      array = new int[10];
    	  }
     
    	  else 
    	  {
    		  array = new int[Integer.parseInt(args[0] ) ]; 
    	  }
     
    	  System.out.printf( "%s%8s\n", "Index", "Value" ); 
     
    	  for ( int counter = 0; counter < array.length; counter++ )
    		 System.out.printf( "%5d%8d\n", counter, array[ counter ] );
     
    	}
    }

    which gives me an output of:

    Please enter a number for the array size: 5

    And it just hangs there. What am I missing? I hope I posted this is in the right subsection, since it is an array issue. Any help or guidance would be appreciated. Thanks!


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    78
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: My program compiles and runs, but doesn't display anything!

    hi,

    Your program asking 2 inputs but you given only one input that is the problem.

    Please remove the below line of code, then try to run it will run.
    int num = input.nextInt(); //unnecessary line, you are not using anywhere

    Why you are getting another input ?? What is the use of above line.??



    Thanks,

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,446
    My Mood
    Mellow
    Thanks
    86
    Thanked 476 Times in 467 Posts

    Default Re: My program compiles and runs, but doesn't display anything!

    Welcome to the forum! Thanks for taking the time to learn to post code correctly.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program compiles and runs, but doesn't display anything!

    Quote Originally Posted by Ganeprog View Post
    hi,

    Your program asking 2 inputs but you given only one input that is the problem.

    Please remove the below line of code, then try to run it will run.
    int num = input.nextInt(); //unnecessary line, you are not using anywhere

    Why you are getting another input ?? What is the use of above line.??



    Thanks,
    I guess I've done so many revisions to the code, that line probably just lingered in there and I didn't notice. But even with that line removed, the output still looks like:

    Please enter a number for the array size: 5
    Index   Value
        0       0
        1       0
        2       0
        3       0
        4       0
        5       0
        6       0
        7       0
        8       0
        9       0

    regardless of what number I put in. Does it have anything to do with these lines?

    if(args.length == 0)
    array = new int[Integer.parseInt(args[0] ) ];

    I tried messing around with them some but it kept throwing some errors.

    Thanks for the warm welcome BTW!

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,446
    My Mood
    Mellow
    Thanks
    86
    Thanked 476 Times in 467 Posts

    Default Re: My program compiles and runs, but doesn't display anything!

    You define an array in 3 different places, the first being completely pointless, because it will be redefined later. Your logic is confused, non-compliant with the assignment's instructions, and out of order.

    The assignment says: "(create an array of) the size defined in the first command line argument. If no command (line) argument is given, then just use 10 as the default size."

    Code that. In pseudo code, it looks something like:

    if args.length == 0
    array = new int[10]
    else
    array = new int[ args[0] ]

    You don't need a Scanner object. Where in the assignment does it say to ask the user for input? Or is that a holdover from the previous assignment? Sometimes it's better to start over.

    Don't let friends code with fried brains. Get some rest.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program compiles and runs, but doesn't display anything!

    ...


    Try this code
    Last edited by copeg; March 19th, 2014 at 08:42 AM. Reason: Removed spoonfeeding

  7. #7
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: My program compiles and runs, but doesn't display anything!

    @mahenaazkhan, welcome to the forums. I recommend reading the forum guidelines and the following: The Problem with Spoon-feeding

Similar Threads

  1. [SOLVED] Program compiles with a cannot find symbol error- what is that?
    By kissyfurs in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 10th, 2013, 11:13 PM
  2. Output doesn't display numbers
    By Sylis in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2012, 10:51 PM
  3. [SOLVED] program compiles & executes, but the numbers ain't right :O
    By crys in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 9th, 2012, 12:06 PM
  4. Replies: 6
    Last Post: November 25th, 2011, 02:58 PM
  5. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 11:46 AM