java - Can't find five lowest values in an array...max works while min array is not assigned values -
java - Can't find five lowest values in an array...max works while min array is not assigned values -
i'm close completing this, need help on finding 5 lowest values text file using arrays. figured out how find 5 highest values, min array find lowest values outputs 5 0's.
output: //obviously dependent on individual text file
total amount of numbers in text file 10
sum is: 1832
1775 14 9 9 7 //max
0 0 0 0 0 //min
any help much 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 integers } for(int j:numbers)//get sum { sum+=j; } (int j=0; j < 5; j++) //finds 5 highest { (int i=0; < numbers.length; i++) { if (numbers[i] > max[j]) { temp = numbers[i]; numbers[i] = max[j]; max[j] = temp; } } } (int j=0; j < 5; j++) //finds 5 lowest...array not assigned values { (int i=0; < 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 " + 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]); } }
try debugging code entering within nested min loop next line:
system.out.println("the value of numbers[i] is: " + numbers[i]);so looks this:
for (int j=0; j < 5; j++) //finds 5 lowest...array not assigned values { (int i=0; < 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'll notice interesting. innermost nested part doesn't start.
try putting line nested max loop in respective location instead... , run fine , show max array values. getting 0 values min array because (other initial assigning) innermost part of nested min loop isn't beingness started somehow, fails run , searched values not assigned min array.
the outer nested parts of min loop run fine if seek debugging them similar line. it's part won't start , something's wrong with:
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 min loop, numbers[i] i=0 i=4 have value of 0 after completing max loop.
you need add together 1 line , utilize int i=5 instead of int i=0 within min loop:
for (int j=0; j < 5; j++) //finds 5 lowest...array not assigned values { min[j] = max[4]; // added line (int i=5; < numbers.length; i++) // alter int i=5 { if (numbers[i] < min[j]) {...
java arrays highest
Comments
Post a Comment