Can't find the smallest five values in the array ... max works and min array does not assign values
I am very close to completing this, I need help finding the five lowest values from a text file using arrays. I figured out how to find the five highest values, but my minimum array to find the lowest values always outputs five 0's.
Conclusion: // clearly depends on a separate text file
The total number of numbers in the text file is 10
The sum is: 1832
1775 14 9 9 7 // max
0 0 0 0 0 // min
Any help is greatly appreciated!
import java.util.Scanner;
import java.io.*;
public class HW3
{
public static void main(String[] args) throws IOException
{
File f = new File("integers.txt");
Scanner fr = new Scanner(f);
int sum = 0;
int count = 0;
int[] max = new int[5];
int[] min = new int[5];
int temp;
while(fr.hasNextInt())
{
count++;
fr.nextInt();
}
Scanner fr2 = new Scanner(new File("integers.txt"));
int numbers[] = new int[count];
for(int i=0;i<count;i++)
{
numbers[i]=fr2.nextInt(); //fills array with the integers
}
for(int j:numbers)//get sum
{
sum+=j;
}
for (int j=0; j < 5; j++) //finds five highest
{
for (int i=0; i < numbers.length; i++)
{
if (numbers[i] > max[j])
{
temp = numbers[i];
numbers[i] = max[j];
max[j] = temp;
}
}
}
for (int j=0; j < 5; j++) //finds five lowest...array not assigned values
{
for (int i=0; i < numbers.length; i++)
{
if (numbers[i] < min[j])
{
temp = numbers[i];
numbers[i] = min[j];
min[j] = temp;
}
}
}
System.out.println("Total amount of numbers in text file is " + count);
System.out.println("Sum is: " + sum);
System.out.println(max[0] + " " + max[1] + " " + max[2] + " " + max[3] + " " + max[4]);
System.out.println(min[0] + " " + min[1] + " " + min[2] + " " + min[3] + " " + min[4]);
}
}
source to share
Try to debug your code by entering the following line inside your nested min loop:
System.out.println ("the value of numbers [i] is:" + numbers [i]);
so it looks like this:
for (int j=0; j < 5; j++) //finds five lowest...array not assigned values
{
for (int i=0; i < numbers.length; i++)
{
if (numbers[i] < min[j])
{
System.out.println("the value of numbers[i] is: " + numbers[i]);
temp = numbers[i];
numbers[i] = min[j];
min[j] = temp;
}
}
}
You will notice something interesting. The innermost nested part doesn't even start.
Try putting this line in a nested max-loop at your respective location, not ... and it will work fine and display the values of the max array. You end up with null values for the minimum array because (other than the primary assignment) the innermost part of the nested min-loop doesn't start in some way, so it doesn't start and look for values that aren't assigned to the mini-array.
The outer nested parts of the min loop work fine if you try to debug them with a similar line. This is the part that won't start and something is wrong:
if (numbers[i] < min[j])
{
System.out.println("the value of numbers[i] is: " + numbers[i]);
temp = numbers[i];
numbers[i] = min[j];
min[j] = temp;
}
(Update) In the min-loop, the numbers [i] from i = 0 to i = 4 have the value 0 after the maximum loop is completed.
You only need to add one line and use int i = 5 instead of int i = 0 inside your min loop:
for (int j=0; j < 5; j++) //finds five lowest...array not assigned values
{
min[j] = max[4]; // added line
for (int i=5; i < numbers.length; i++) // change to int i=5
{
if (numbers[i] < min[j])
{...
source to share
Your mini-array will be initialized to zero values. Thus, the values in numbers will always be higher (if there are no negative values).
I suggest you initialize min [j] with numbers [0] before the inner loop.
for (int j=0; j < 5; j++) //finds five highest
{
min[j] = numbers[0]; // Add this line
for (int i=0; i < numbers.length; i++)
{
source to share
Just curious, Could you just sort it (using quicksort) to select the top five and the bottom five? - if you can use sorting i think this should work then
int sum = 0;
int count = 0;
int[] max = {Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};
int[] min = {Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE};
int temp;
int visited[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int j : numbers)// get sum
{
sum += j;
}
int tempindex;
for (int j = 0; j < 5; j++) // finds five highest
{
for (int i = 0; i < numbers.length; i++) {
if (visited[i] != 1) {
if (numbers[i] > max[j]) {
max[j] = numbers[i];
tempindex = i;
}
}
}
visited[tempindex] = 1;
}
for (int j = 0; j < 5; j++) // finds five lowest...array not assigned
// values
{
for (int i = 0; i < numbers.length; i++) {
if (visited[i] != 1) {
if (numbers[i] < min[j]) {
min[j] = numbers[i];
tempindex = i;
}
}
}
visited[tempindex] = 1;
}
source to share
As the other answer says, your problem is that you were not counting arrays starting with 0. In Java, it sets the default values for this data structure. For primitives, this will usually be 0 or false. However, when you go into data structures, you will have problems with null pointer exceptions if you fail to initialize your objects. For this reason, I urge you to get used to setting values in your data structures before using them. This will save you a lot of debugging time in the future.
If you know the values in advance, you can set them manually using {0,0,0,0,0} notation, or you can initialize with a for loop:
for(int i = 0; i < array.length; i++)
array[i] = init_value;
I would recommend that you also try to consolidate as much as possible. For example, in your code, you are looking at the same data 4 times:
1) read integers from file into integer array
2) sum all numbers in an array of integers
3) find max
4) find min
I'm not sure if you have covered the features yet, but one example of consolidating this might look like this:
while(fr2.hasNextInt()){
int i = fr2.nextInt();
sum += i;
checkHighest(i);
checkLowest(i);
}
Then you define those functions and put the meat somewhere else. This allows you to only worry about the hinges in one place.
source to share
You have 2 problems. Tom Elliott explained first.
The second problem is that also the max [] array is initialized to 0, and when looking for maximum values, you change the value from the max array (which is 0) with the value from the array of numbers, so the array of numbers is filled with 0s.
A quick solution (though not the best) would be to copy the array of numbers into the temp array and use that temperature when looking for the minimum values.
If you don't understand exactly what I said, try printing an array of numbers after you've found the 5 maximum values.
source to share