• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array i/o and sorting

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at your code again. You never actually place any values into your array.

Henry
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, thank you, Junilu Lacar! You have made my day. My program works!!!

Have a great weekend!
Christy
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic