Results 1 to 5 of 5
  1. #1
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Array length and printing out uninitialized array.

    I am having trouble setting array length. My program reads in a file and reads integers only. However, I can print out all the integers, but once the last integer is read, it prints out 'null' until the declared array length ends.
    below is my code:
    Java Code:
      import java.util.Scanner;
       import java.io.File;
       import java.util.StringTokenizer;
       import java.util.InputMismatchException;
       import java.io.FileNotFoundException;
       import java.util.NoSuchElementException;
    
    	
    
        public class KashiwabaraNicole3
       {
         /*******************************************************************************
         *  Outputs integers from user input external files.
         ********************************************************************************/
        
           public static void main(String[] commandlineArguments)throws InputMismatchException 
          {
             if(commandlineArguments.length == 0)
             {
                System.out.println("Please enter the file name " +
                   "as the 1st commandline argument.");
             }
             else
             {
                Integer[] array = KashiwabaraNicole3.readFileReturnIntegers(commandlineArguments[0]);
                KashiwabaraNicole3.printArrayAndIntegerCount(array, commandlineArguments[0]);
             }
          }	
           public static Integer []readFileReturnIntegers(String inputFile)
          {
            
             Integer [] array = new Integer [8000];
            
             Integer size = new Integer(0);
             File file = new File(inputFile);
                
             Scanner scanFile = null;
                
             try {
                scanFile = new Scanner(file);
             } 
                 catch (FileNotFoundException exception) {
                      
                   System.out.print("ERROR: File not found for \"");
                   System.out.println(inputFile +"\"");
                }  
                       
             if(scanFile != null)
             {
               
                while (scanFile.hasNextLine()) 
                {   
                   try
                   {                       
                      int element = scanFile.nextInt();
                      array[size] = element;
                      ++size;								  
                   }
                       catch (InputMismatchException exception)
                      {
                         scanFile.next();
                      } 
                      catch (NoSuchElementException exception)
                		{
                		  scanFile.next();
                		}        
                }
             }
          
             return array;          
          }
          
           public static void printArrayAndIntegerCount(Integer [] array, String inputFile)
          {
             int num = 0;     
             System.out.println("number of integers in file " + inputFile + " = " + array.length);
          
             for (int i = 0; i < 10; i++)
             {
                System.out.println("index = " + i + ", element = "+ array[i]);
                
             }
          
          
          }
          	
          																																																																								
          	     
       }
    The file name is inputted into the args.
    Ive attached the files.
    My output for electricity:
    number of integers in file electricity.txt = 8000
    index = 0, element = 1877
    index = 1, element = 1923
    index = 2, element = 1879
    index = 3, element = 2000
    index = 4, element = null
    index = 5, element = null
    index = 6, element = null
    index = 7, element = null
    index = 8, element = null
    index = 9, element = null.....(until index = 8000, element = null)

    Thank you so much in advance for your help!!! :)
    Attached Files Attached Files
    Last edited by nicolek808; 09-07-2009 at 01:20 PM.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    as length of array is 8000
    Java Code:
    Integer [] array = new Integer [8000];
    if you loop though the array, 8000 output print
    i suggest you LinkedHashMap, Vector etc to store the integer
    or you can try to return value of "size", so that you can loop for 0 to < size only
    Last edited by mtyoung; 09-07-2009 at 01:02 PM.

  3. #3
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you for your suggestion. :)

    I tried using size as the array length, however, I cannot pass it into the method.

    I believe it has something to do with passing 'element' into the array.
    I have re-written as:

    Java Code:
    array[size] = scanFile.nextInt();
                      ++size;
    and now I get a NoSuchElementException.

    Any Suggestions for this?
    Last edited by nicolek808; 09-07-2009 at 01:19 PM.

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

    Default

    1) As noted above, why not use a varaible sized collection instead of a fixed-sized array?
    2) If you must use a large fix sized array, (sorry if I'm oversimplifying), why not check if the element is null before printing it? If not null, print it, if null, break out of the for loop.

  5. #5
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you everyone!

    I did figure out the program and I ended up using something similar to fubarable's suggestion. :)

Similar Threads

  1. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 09:57 PM
  2. printing array
    By mayhewj7 in forum New To Java
    Replies: 6
    Last Post: 02-12-2009, 03:01 PM
  3. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 11:58 AM
  4. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:15 PM
  5. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:42 PM

Posting Permissions

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