Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array i/o and sorting  RSS feed

 
Christy Wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at your code again. You never actually place any values into your array.

Henry
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christy Wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. So, what that, how do I get the elements into the array? Sorry to keep asking questions.

Christy
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This statement assigns a value to element n of array arr:

arr[n] = someValue;
 
Christy Wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, thank you, Junilu Lacar! You have made my day. My program works!!!

Have a great weekend!
Christy
 
Dilip Fernandez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!