| Author |
How to determine the length of file contents
|
Mohit Hyd Kumar
Greenhorn
Joined: Jul 31, 2011
Posts: 1
|
|
Hi all,
I am writing a program for binary search on contents of a file.
The steps involved in it are:
1. Have to read a file[Inputing the filename from the user] - using bufferedReader.readLine() method[bufferedReader = new BufferedReader(new InputStreamReader(new FileInputSteam(filename)))]
2. Have to search the number[entered by user] in the contents of file [using Binary Search]
3. Have read from the file and input the contents in a int array.
The problem arises here:
The array i formed and then sorted[2 different methods] have some zero values[as I am not able to determine actually how many numbers are there in the file, so took an arbitrary int array of size 20, but the file contains only 6 numbers, hence there are 14 "0" values].
However besides the values, the method works flawlessly
Any solutions how to determine how many numbers are present in the file[cannot use fileInputStream.read() == -1 as stated above].
Thanks in advance
|
 |
Dawid Skrzypczynski
Ranch Hand
Joined: Jun 08, 2011
Posts: 50
|
|
|
maybe you should use ArrayList ?
|
 |
James E Baker
Greenhorn
Joined: Jul 28, 2011
Posts: 23
|
|
Mohit Hyd Kumar wrote:
Any solutions how to determine how many numbers are present in the file[cannot use fileInputStream.read() == -1 as stated above].
Thanks in advance
So, the user will never enter '0' as a valid value?
If you want to count how many values in an array are NOT zero, then you could do
Is that what you're looking to do?
|
 |
 |
|
|
subject: How to determine the length of file contents
|
|
|