Building array from .txt file questions

I am working on a program to:

1. creates a main that opens a file
2. reads an int from the file that is the size of the array
3. allocates the array to the size of the int that was just read
4. reads all the ints into the array that was just allocated
5. calls the findSmallest method to find the smallest value
6. prints out the value of the smallest element in the array

The problem areas for me are items 3, 4, and 5 on this list.

My method to find the smallest element of the array is:

public static int findSmallest(int[]numbers,int index){
if (index + 1 == numbers.length){
return numbers[element];
}
else {
return Math.min(numbers[index], findSmallest(numbers,index +1));
}
}

My file input initializations are:

FileInputStream fis = new FileInputStream("mydata.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

I want to use a try-catch to make it all happen.

try {

while ( (record=dis.readLine()) != null ) {

}

So, as you can see what I am stuck on is how to actually read from the file, build the array, fill the array and so on.

I understand what I need to do but the code to make it happen escapes me. I need to: read the first int from the .txt file, build an array with elements equal to the first int from the .txt file, fill the array with the remaining ints from that .txt file.

Any help would be appreciated.

Thanks,
ateo
[1646 byte] By [ateo] at [2007-11-20 10:08:49]
# 1 Re: Building array from .txt file questions
If I'm catching what you want to do...

Say this is the first .txt's contents

20
53
145
1422
14252

You want to take "20", make that the length of a new Array, and store the remaining Integers into this new Array? If so...

Something like this:

Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("mydata.txt")));

while (s.hasNext()) {
storageArray[count] = (s.next()); //Store all elements of .txt to an Array.
count +=1;
}
} finally {
if (s != null) {
s.close();
}
}

int count2 = 1;
int count3 = 0;
int[] newArray = new int[storageArray[0]]; //Declare Array w/ size of first int from .txt.
while (count2 <= storageArray[0]) { //Store remaining elements into the new array.
newArray[count3] = storageArray[count2];
count2 += 1;
count3 += 1;
}

Something like that maybe? I'm no coding guru, but that would probably work.
omgwtfbbq9999 at 2007-11-10 2:14:34 >
# 2 Re: Building array from .txt file questions
Thanks for the reply.

I am getting some errors with the code I have.

public static int findSmallest(int[]numbers,int index){
if (index + 1 == numbers.length){
return numbers[element];
}
else {
return Math.min(numbers[index], findSmallest(numbers,index +1));
}
}

This gives me errors with cannot find symbol variant element.

Scanner s = null;

try {
s = new Scanner(new BufferedReader(new FileReader("input.txt")));

while (s.hasNext()) {
storageArray[count] = (s.next()); //Store all elements of .txt to an Array.
count +=1;
}
int count2 = 1;
int count3 = 0;
int[] newArray = new int[storageArray[0]]; //Declare Array w/ size of first int from .txt.

while (count2 <= storageArray[0]) { //Store remaining elements into the new array.
newArray[count3] = storageArray[count2];
count2 += 1;
count3 += 1;
}
}
catch (FileNotFoundException e) {
System.err.println(s + ": cannot be opened for reading");
System.exit(0);
}
catch (IOException e) {
System.err.println("File system error on "+ s);
System.exit(0);
}
finally {
if (s != null) {
s.close();
}

This gives me errors with cannot find variable storageArray and count.

I'm sure this is something really obvious (or so it would seem) that I'm missing. Can someone point me in the right direction?

Thanks.
ateo at 2007-11-10 2:15:37 >
# 3 Re: Building array from .txt file questions
When you get 'Cannot find symbol' errors for variables in your code, it's usually because you haven't declared them, or they're not in scope (e.g. local variables declared in one method and used in another).

I'm guessing you haven't declared them. Where is variable 'element' declared? what type is it? What about storageArray and count?

You have to declare variables before you can use them.

Programs must be written for people to read, and only incidentally for machines to execute...
H. Ableson & G. Sussman
dlorde at 2007-11-10 2:16:44 >
# 4 Re: Building array from .txt file questions
Yeah, sorry for not declaring them for yah. Usualy have them delcared right at the start of the method for ease of finding.
omgwtfbbq9999 at 2007-11-10 2:17:38 >
# 5 Re: Building array from .txt file questions
Ok. I am now back in the saddle to try to get this done before midnight so I don't fail my programming course for this semester. I bombed the final (I needed a 37% on the final to pass the class without finishing this project (which is giving me massive difficulties - but I have 100% on all of the other projects) - and I'm not sure I got that 37%.

Not your problem, but I urgently and desperately need help.

Here's the code I have at the moment.

public static int findSmallest(int[]numbers,int index){
int[] element = new int[100];
if (index + 1 == numbers.length){
return numbers[element];
}
else {
return Math.min(numbers[index], findSmallest(numbers,index +1));
}
}

I get incompatible type compile-time error with this line:

return numbers[element];

// main(): application entry point
public static void main(String[] args) {

//FileInputStream fis = new FileInputStream("mydata.txt");
//BufferedInputStream bis = new BufferedInputStream(fis);
//DataInputStream dis = new DataInputStream(bis);

Scanner s = null;

try {
int count = 0;
s = new Scanner(new BufferedReader(new FileReader("input.txt")));
int[] storageArray = new int[100];

while (s.hasNext()) {
storageArray[count] = (s.next()); //Store all elements of .txt to an Array.
count +=1;
}
int count2 = 1;
int count3 = 0;
int[] newArray = new int[storageArray[0]]; //Declare Array w/ size of first int from .txt.

while (count2 <= storageArray[0]) { //Store remaining elements into the new array.
newArray[count3] = storageArray[count2];
count2 += 1;
count3 += 1;
}
}
catch (FileNotFoundException e) {
System.err.println(s + ": cannot be opened for reading");
System.exit(0);
}
catch (IOException e) {
System.err.println("File system error on "+ s);
System.exit(0);
}
finally {
if (s != null) {
s.close();
}
}
}

I get an incompatible types error with this line:

storageArray[count] = (s.next()); //Store all elements of .txt to an Array.

As always, I truly appreciate any help that can be provided.

Thanks,
ateo

P.S. I suspect the code has problems beyond these two compile-time errors that are more significant.
ateo at 2007-11-10 2:18:43 >
# 6 Re: Building array from .txt file questions
I get incompatible type compile-time error with this line:

return numbers[element];
You've declared 'element' to be an array of ints, but you're trying to use it as an index into an array of ints, 'numbers'. Just look at that first piece of code you posted, and step through it by hand, with pencil and paper if necessary. It's a short piece of code, and you know what it's supposed to do, so you should be able understand what it should be doing, what it actually does, and why it doesn't compile.
I get an incompatible types error with this line:
storageArray[count] = (s.next()); //Store all elements of .txt to an Array.
Scanner.next() returns a String. You are trying to store this String into an element of an array of ints. You can't store a String where an int is expected. If the String is a text representation of a number, you have to convert it into an int before putting it in the int array. You could use the Integer.parseInt(..) method to do this.

When posting a report of an error, please remember to post the full text of the error message.

If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anon.
dlorde at 2007-11-10 2:19:44 >
# 7 Re: Building array from .txt file questions
Is there no way to scan the file as ints directly into an int array?

Could I cast what is scanned into int type in order to avoid using that method you spoke of?
ateo at 2007-11-10 2:20:48 >
# 8 Re: Building array from .txt file questions
Alright, I give up.

I don't have time to finish this.

Guess I'll just face the music.
ateo at 2007-11-10 2:21:49 >
# 9 Re: Building array from .txt file questions
Is there no way to scan the file as ints directly into an int array?If you spend a minute to look at the JavaDocs for Scanner (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html), you'd see that there is a method to get the next int.
Could I cast what is scanned into int type in order to avoid using that method you spoke of?No, but see above. Why do you want to avoid using that method?

The purpose of computing is insight, not numbers...
R. Hamming
dlorde at 2007-11-10 2:22:46 >
# 10 Re: Building array from .txt file questions
Alright, I give up.

I don't have time to finish this.

Guess I'll just face the music.Why does that not suprise me?

Act in haste and repent at leisure; code too soon and debug forever...
R. Kennington
dlorde at 2007-11-10 2:23:42 >
# 11 Re: Building array from .txt file questions
I hate it when a programmer gives up :( I hate it even more when you've helped that programmer and they give up after it. Programming is not a difficult thing, you just have to get a mindset for what you want the program to do, sort out how it should do it, and think of a way to code it that way.

This has helped me a lot (http://www.leepoint.net/notes-java/)

All you have to do to store what you take out of that .txt file is to parse it to an int.

I think it goes something like:

Integer.parseInt(whatYouWantToChangeToInt);

You can do a do{}while() statement and make it just a few lines of code as well.
omgwtfbbq9999 at 2007-11-10 2:24:52 >