randomname
randomname

Reputation: 275

How do you determine size of string array java?

How do i read a file and determine the # of array elements without having to look at the text file itself?

String temp = fileScan.toString();
String[] tokens = temp.split("[\n]+");
// numArrayElements = ?

Upvotes: 0

Views: 14343

Answers (2)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Use the length property of the array:

int numArrayElements = tokens.length;

Upvotes: 5

tbodt
tbodt

Reputation: 16987

The proper expression is tokens.length. So, you can assign numArrayElements like this:

int numArrayElements = tokens.length;

This counts the number of elements in the tokens array. You can count the number of elements in any array in the same way.

Upvotes: 2

Related Questions