By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,784 Members | 3,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,784 IT Pros & Developers. It's quick & easy.

how can I open up my file (numbers.txt) and read it into an array?

P: 1
Here's what I have so far. I just cant figure out how to read my file into an array. If you have any idea how to do this PLEASE help me!!!
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class bored{
  5.  
  6.    public static void main(String[] args) throws IOException 
  7.        {
  8.  
  9.                int [] newArray = new int [1000];
  10.  
  11.             try{
  12.             Scanner stdin = new Scanner(System.in);
  13.             System.out.print("Enter file name: ");
  14.             String filename = stdin.next();
  15.             System.out.println("file name :"+filename);
  16.             Scanner filein = new Scanner( new File(filename) );
  17.             int numberCharacters = 0;   
  18.  
  19.             while ( filein.hasNext() ) {
  20.                  if(filein.hasNextInt())
  21.                      numberCharacters++;
  22.                     System.out.println(filein.next());
  23.                   }
  24.             System.out.println();
  25.             System.out.println("Number of integers in " + filename+ ": " + numberCharacters);
  26.             System.out.println();
  27.             }catch(Exception e){System.out.println(e);}
  28.         }
  29. }
  30.  
Nov 4 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
you use nextInt() method from Scanner to read the integers and assign them to the next element in the array, e.g.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class bored{
  5.  
  6.    public static void main(String[] args) throws IOException 
  7.            {        
  8.                     int [] newArray = new int [1000];
  9.  
  10.                         try{
  11.                         Scanner stdin = new Scanner(System.in);
  12.                 System.out.print("Enter file name: ");
  13.                     String filename = stdin.next();
  14.                         System.out.println("file name :"+filename);
  15.             Scanner filein = new Scanner( new File(filename) );
  16.                         int numberCharacters = 0;   
  17.  
  18.             while ( filein.hasNext() ) {
  19.                                  if(filein.hasNextInt())
  20.                                     System.out.println(newArray[numberCharacters++]=filein.nextInt());
  21.                   }
  22.                         System.out.println();
  23.                 System.out.println("Number of integers in " + filename+ ": " + numberCharacters);
  24.                         System.out.println();
  25.                for(int i=0; i<numberCharacters; i++)
  26.                         System.out.println(newArray[i]);
  27.                         }catch(Exception e){System.out.println(e);}
  28.         }
  29. }
  30.  
Nov 5 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.