aspose file tools*
The moose likes I/O and Streams and the fly likes Reading array elements from a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading array elements from a file." Watch "Reading array elements from a file." New topic
Author

Reading array elements from a file.

Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Hello. I am new to Java and currently having a hard time mastering File I/O. My task is to read the information for an array from a file, print the elements, and then sort for and display the smallest element. I have been unsuccessful in reading the elements into the array. Can somebody please help me?

Here is what I have so far. Please let me know if you need more information.

public static void main(String[] args) throws IOException {
int[] array;
int items = 0;
int value = 0;

try {
BufferedReader br = new BufferedReader (
new FileReader("integers.dat"));

items = Integer.parseInt(br.readLine());
array = new int[items];

for (int i = 0; i < array.length; i++) {
value = Integer.parseInt(br.readLine());
array[i] = ??;
System.out.println(array[i]);
}

// Call the method to sort the array.
findSmallest(array);

// Print the array.
System.out.println("The smallest value is " + array[0]);
} catch (IOException ioe) {
System.out.println("Error in IO");
System.out.println("Message is:" + ioe.getMessage());
}
} // end method

Any and all help would be greatly appreciated.

Thanks!
Christy
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

The "??" ought to be simply "value", but I'm curious as to why you need to ask that; it's just the same as reading "items" earlier.

You don't close your file anywhere; good practice dictates that you should call br.close() after you're through with it.

Do you need help with "findSmallest()"?


[Jess in Action][AskingGoodQuestions]
Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Ernest-

Thank you for the welcome and the help.

I wasn't sure if I could add the elements in the same manner or not. Still learning!

Thanks for the tip on closing the file. I hadn't gotten that far in the learning either.

Do you need help with "findSmallest()"? I am not sure about that part yet, I am trying to get one thing down at a time, but thanks again for the offer.

I am going to return to my program with your much needed guidance and see what happens.

Thanks,
Christy
Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Hello, again. Now I don't even know what to ask. My program is telling me that the smallest value is 0. (It should be 3). Where am I going wrong here?

My file looks like this:

5
25
15
3
7
31

It is called "integers.dat".

Here is my code:

/* File: Find_Smallest_Elements.java
* Author: Christina Wilson
* Date: 05-01-2005
* Purpose: Sorts for and displays smallest value
* in an array.
*/

import java.io.*;

public class Find_Smallest_Elements {

//method to sort array and find smallest elements
public static void findSmallest(int[] array) {
for (int i = 0; i < (array.length-1); i++) {

//position of smallest element
int min = i;

//for loop to locate smallest element
for (int j = i; j < (array.length); j++) {
if (array[j] < array[min])
min = j;
}// end for

//moves elements so that they are eventually
//arranged in order from smallest to largest
int temp = array[min];
array[min] = array[i];
array[i] = temp;

} // end for
} // end method

//main method to load array from file
//and to request smallest element
public static void main(String[] args) throws IOException {
int[] array;
int items = 0;
int value = 0;

try {
BufferedReader br = new BufferedReader (
new FileReader("integers.dat"));

items = Integer.parseInt(br.readLine());
array = new int[items];

for (int i = 0; i < array.length; i++) {
value = Integer.parseInt(br.readLine());
array = new int[value];
}

br.close();

// Call the method to sort the array.
findSmallest(array);

// Print the array.
System.out.println("The smallest value is " + array[0]);
} catch (IOException ioe) {
System.out.println("Error in IO");
System.out.println("Message is:" + ioe.getMessage());
}
} // end method
} // end class


Any ideas?

Thanks, again,
Christy
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is an example of what happens when you post the same question to more than one forum -- you've got people answering in both forums, and it appears in the other thread you've got all the answers you need, so my answers here are wasted. Please don't post the same question in more than one place. This thread continues here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading array elements from a file.