Output should be as show below:
and files are attached.
1. Here is example output for input file electricity.txt:

number of integers in file "electricity.txt" = 4
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000


2. Here is example output for input file groceries.csv:

number of integers in file "groceries.csv" = 6
index = 0, element = 3
index = 1, element = 12
index = 2, element = 1
index = 3, element = 1
index = 4, element = 5
index = 5, element = 1


3. Here is example output for input file 1000.txt:

number of integers in file "1000.txt" = 1001
index = 0, element = 1000
index = 1, element = 2
index = 2, element = 3
index = 3, element = 5
index = 4, element = 7
index = 5, element = 11
index = 6, element = 13
index = 7, element = 17
index = 8, element = 19
index = 9, element = 23
index = 10, element = 29.....
............................................
.....index = 995, element = 7877
index = 996, element = 7879
index = 997, element = 7883
index = 998, element = 7901
index = 999, element = 7907
index = 1000, element = 7919

I am having trouble reading the integers an putting them into an array...below is the code I have so far.

import java.util.Scanner;
   import java.io.File;
   import java.util.StringTokenizer;
   import java.util.InputMismatchException;
   import java.io.FileNotFoundException;

   public class KashiwabaraNicole3test
   {
     /*******************************************************************************
     *  Outputs integers from user input external files.
     ********************************************************************************/
    
       public static void main(String[] commandlineArguments) {
      
      
      //Error Checking For Command Line Arguments..
         if(commandlineArguments.length == 0){
            System.out.println("Please enter the file name " +
               "as the 1st commandline argument.");
         }
         else{
         //connect to file (does NOT create new file)
            for(int i=0; i<commandlineArguments.length; i++)
            {System.out.println();
               File file = new File(commandlineArguments[i]);
            
               Scanner inputFromFile = null;
            
               try {
                  inputFromFile = new Scanner(file);
               } 
                   catch (FileNotFoundException exception) {
                  //Print error message.
                  //In order to print double quotes("), 
                  //the escape sequence for double quotes (\") must be used.
                     System.out.print("ERROR: File not found for \"");
                     System.out.println(commandlineArguments[0]+"\"");
                  }        
            //if made connection to file, read from file
                       //keeps looping if file has more lines..
               while (inputFromFile.hasNextLine()) {
               //get a line of text..
                  String line = inputFromFile.nextLine();
			try
         { 
            Integer element = scan.nextInt();
            for (int i=element;i<arraySize;i++)
            {
              element[i] = element[i]; 
            } 
         }	
             catch(InputMismatchException exception)
            {
            }   

        }       
               
               String [] elementList = KashiwabaraNicole3test.createOutput(file);
               KashiwabaraNicole3test.displayArray(elementList);
            	
            	
            }//end of "if" for connecting to file
         }//end of "else" for commandlineArguments
      }//end of main() method		
   	
       public static String [] createOutput (File myFile)
      {
		//need to declare "array" 
         Scanner scan = new Scanner(myFile);
			
         try
         { 
            Integer element = scan.nextInt();
            for (int i=element;i<arraySize;i++)
            {
              element[i] = element[i]; 
            } 
         }	
             catch(InputMismatchException exception)
            {
            }   
         return arraySize;   	
        
      
      }
       public static void displayArray(String [] anArray)
      {
         int num = 0;
         Integer lengthOfArray = anArray.length;
         System.out.println("number of intergers in file " + commandlineArguments + " = " + lengthOfArray);
      	 
      	
         for (int i = 0; i < lengthOfArray; index++)// Initialize the array values
         {
           
            Integer lengthOfArray = anArray[i].length(); 
            System.out.println("index = " + num +  " , element = " + anArray[i]);
            num++;
         }  
           
      }
   }

bbcode

Try modifying createOutput method like shown below

public static String [] createOutput (File myFile)
      {
		//need to declare "array" 
         Scanner scan = new Scanner(myFile);
String [] myArray;
			
         try
         { 

            myArray = new String[1000]; //instead of 1000 use some appropriate method to find out total number of integers in the scan
            
              int i=0;
              while(scan.hasNextInt())
              {
                     myArray[i] = scan.nextInt().toString();
                     i++;
              }
         }	
             catch(InputMismatchException exception)
            {
            }   
         return myArray;   	
        
      
      }

thank you for your suggestion. I have tried it however, i get a error message at 'myArray = scan.nextInt().toString();' "int cannot be dereferenced"

If you check the API, the scan.nextInt() returns an int value, so it doesn't have a toString() method.
Objects have that method. "int", or "float" and the others are not Objects.
Try this:

myArray[i] = String.valueOf( scan.nextInt() );

I revised my code, now I cannot seem to read the integers into the array.

See code below:

/*************************************************************************************
* Instantiates array of integers from a user inputted external file.   
* @author Kashiwabara, Nicole
* @assignment ICS 211 Assignment 3
* @date August 24, 2009
*************************************************************************************/
   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 [10000];
         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[element]=element;
               	                          
               }
                   catch (InputMismatchException 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 < array.length; i++)
         { 
            System.out.println("index = " + i+ ", element = "+array[i] );
            num++;
         }
      }
      	
      																																																																								
      	     
   }

If you want to store an element sequentically include counter variable,

if(scanFile != null)
         {
            int i=0; // counter 
            while (scanFile.hasNextLine()) 
            {   
               try
               {                       
                  int element = scanFile.nextInt(); 
                  array[i++]=element;
               	                          
               }
                   catch (InputMismatchException exception)
                  {
                     scanFile.next();
                  } 
            }
         }

I don't see any problem to assign an element at element^th^ position.

how would i pass that array to the method shown below? I want the output be as shown:
index = 0, element = 1877
index = 1, element = 1923
index = 2, element = 1879
index = 3, element = 2000
however, this is my output:
....(starts from index = 1 to
index = 8503, element = null
index = 8504, element = null
index = 8505, element = null
index = 8506, element = null
index = 8507, element = null
index = 8508, element = null
index = 8509, element = null
index = 8510, element = null
index = 8511, element = null .... index = 10000, element = null)

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 < array.length; i++)
         
         {
         
            System.out.println("index = " + i+ ", element = "+array[i] );
         
            num++;
         }

by the way, the desired output is read from 'electricity.txt'

Content of electricity.txt is,

SCIENCE and ELECTRICITY
by Dave Barry
TODAY'S SCIENTIFIC QUESTION IS: What in the world is electricity and
where does it go after it leaves the toaster?........

How can I size my array based on the file input? The array is orignally sized to 10000, and when it out puts, it I get a 'null' result if it is not an integer.

Also, how do I store the read integers into an array?
I am able to compile and run, however, my output doesnt have an array in readFileReturnIntegers method.

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 [10000];
        
      
         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();
                  
                  System.out.println(element);                        
               								  
               }
                   catch (InputMismatchException exception)
                  {
                     scanFile.next();
                  }         
            }
         }
      
         return array;          
      }
      
       public static void printArrayAndIntegerCount(Integer [] array, String inputFile)
      {
         Integer lengthOfArray = array.length;
      
         System.out.println("number of integers in file " + inputFile + " = " + lengthOfArray);
      
         for (int i = 0; i < lengthOfArray; i++)
            
         
         {
            
            System.out.println("index = " + i + ", element = "+ array[i]);
         
            
         }
      
      
      }
      	
      																																																																								
      	     
   }

You lost same code after

integer element = scanFile.nextInt();
.....

Look to adatapost#6 reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.