Form array from values in file

I have three consecutive integers stored in a file.
Please tell me how I can store these values in an array A[3] dynamically.

If possible, please give me some sample code.

Thanks!
dshrenikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
int [] A = new int[3];
DataInputStream in = new DataInputStream(new FileInputStream("filename.txt");
String buff = null;
int count=0;
while((buff=in.readLine()) != null){
A[count] = Integer.parseInt(buff);
count++;
}
in.close();
for_yanCommented:

You'll also need to catch an Exception:

int [] A = new int[3];
try{
DataInputStream in = new DataInputStream(new FileInputStream("filename.txt");
String buff = null;
int count=0;
while((buff=in.readLine()) != null){
A[count] = Integer.parseInt(buff);
count++;
}
in.close();
} catch(Exception ex){
System.out.println("error " + ex.toString());
ex.printStackTrace();
}

This asumes that integers are each on its own line.
what means dynaamically?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dshrenikAuthor Commented:
I think that should be good. Thanks!
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

dshrenikAuthor Commented:
I'm sorry, I need to have a 3X3 array.
Can you tell me how the array needs to be defined. I''ll take care of the rest.
for_yanCommented:
Actually I never use two-diimensional arrays
I guess you can do something like that
(in the assumption that there are 3 values per line spearated by spaces)

int[][] A;

A= new int[3][3];

try{
DataInputStream in = new DataInputStream(new FileInputStream("filename.txt");
String buff = null;
int count=0;
while((buff=in.readLine()) != null){
StringTokenizer t = new StringTokenizer(buff);

A[count] [0]= Integer.parseInt(t.nextToken());
A[count] [1]= Integer.parseInt(t.nextToken());

A[count] [2]= Integer.parseInt(t.nextToken());


count++;
}
in.close();
} catch(Exception ex){
System.out.println("error " + ex.toString());
ex.printStackTrace();
}

Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.