This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array i/o and sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array i/o and sorting" Watch "Array i/o and sorting" New topic
Author

Array i/o and sorting

Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Hello. I am slowly but (somewhat) surely trying to learn Java. My currnet task is to read the information for an array from a file, print the array, and then sort and display the smallest element. My program lacks the method/code to print the entire element because I am having bigger issues. My program keeps telling me that the smallest value is 0 when it should be 3. Can anyone tell me where I am going wrong?

The file contains:

5 (number of elements, right?)
25
15
3
7
31

My code so far is:

/* 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


Help, please!

Thanks
Christy
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18916
    
  40

Take a look at your code again. You never actually place any values into your array.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4786
    
    8

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

Each iteration of this loop creates a new array of ints (size == value). int arrays will always be initialized with 0's.
[ May 06, 2005: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Thanks for the info. So, what that, how do I get the elements into the array? Sorry to keep asking questions.

Christy
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4786
    
    8

This statement assigns a value to element n of array arr:

arr[n] = someValue;
Christy Wilson
Greenhorn

Joined: May 06, 2005
Posts: 7
Thank you, thank you, Junilu Lacar! You have made my day. My program works!!!

Have a great weekend!
Christy
Dilip Fernandez
Greenhorn

Joined: Jan 03, 2003
Posts: 4
Using an array is conceptually messy . Why not use a data structure from the Collections framework. You have the freedom to do pretty much what you want. Try this using an ArrayList.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array i/o and sorting