• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine the length of file contents  RSS feed

 
Mohit Hyd Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe you should use ArrayList ?
 
James E Baker
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!