Results 1 to 4 of 4
Thread: read input file into array
- 04-22-2011, 11:33 AM #1
Member
- Join Date
- Apr 2011
- Location
- Glasgow
- Posts
- 5
- Rep Power
- 0
read input file into array
I am trying to put the contents of an input file into an array. I have searched online and the only examples I can find are for arrayLists. I cant use an arrayList for this example. I would really appreciate your help!! Thanks
Last edited by randoms:); 04-22-2011 at 10:49 PM.
- 04-22-2011, 05:13 PM #2
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
The following code I wrote enables you to read input from a file into an array:
I tried the code on an input file and it worked.Java Code:import java.util.*; import java.io.*; public class example3 { public static void main (String args[]) throws FileNotFoundException { Scanner scan = new Scanner(new File(args[0])); //provide file name from outside int counter =0; //keep track of how many integers in the file while(scan.hasNextInt()) { counter++; scan.nextInt(); } Scanner scan2 = new Scanner(new File(args[0])); int a[] = new int[counter]; for(int i=0;i<counter;i++) { a[i]=scan2.nextInt(); //fill the array with the integers } } }
- 04-22-2011, 10:46 PM #3
Member
- Join Date
- Apr 2011
- Location
- Glasgow
- Posts
- 5
- Rep Power
- 0
Thank you very much :)
- 04-23-2011, 08:52 AM #4
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Read user input into integer array
By varunb in forum New To JavaReplies: 12Last Post: 07-09-2010, 12:50 PM -
read only double input from text file
By napi1234 in forum New To JavaReplies: 6Last Post: 06-28-2010, 04:06 PM -
Read File into 2d array
By almjodla in forum New To JavaReplies: 8Last Post: 03-23-2010, 02:55 PM -
file input: array of integers
By hannes in forum New To JavaReplies: 8Last Post: 01-27-2010, 03:44 PM -
read a specific line in an input file
By sara12345 in forum Advanced JavaReplies: 7Last Post: 01-03-2010, 10:40 PM


LinkBack URL
About LinkBacks

Bookmarks