I'm creating a class and methods and calling them in the main method. It's to find avg/median/total etc etc of an array.
import java.util.Arrays; public class ArrayOps1D { public static int getTotal(int[] scores) { int total = 0; for(int j = 0; j <scores.length; j++) { total = total + scores[j]; } return total; } public static int getAverage(int[] scores){ int total = 0; int average = 0; for(int i = 0; i < scores.length; i++) { total += scores[i]; average = (total / scores.length); } return average; } public static int getLowest(int[] scores) { int lowest = 100; for(int i = 0; i < scores.length; i++) { if (scores[i] < lowest) { lowest = scores[i]; } } return lowest; } public static int getHighest(int[] scores) { int highest = 0; for(int i = 0; i < scores.length; i++) { if(scores[i] > highest) { highest = scores[i]; } } return highest; } public static int getMedian(int[] scores) { int median = 0; Arrays.sort(scores); int middle = (scores.length / 2); if ( 0 == scores.length % 2) median = (scores[middle] + scores[middle+1])/2; else if (0 != scores.length % 2) { median = scores[middle]; } return median; } public static int getPosition(int[]scores, int number) { int holder = 0; for (int i = 0; i < scores.length; i++) { if(number == scores[i]) { holder = i; } } return holder; } }
and also this...
import java.io.*; import java.util.Arrays; public class staticMethods{ public static void main(String[] args) throws IOException{ File dataFile = new File("G:\\COSC 1337\\Homework\\Static Methods HW\\Scores.txt"); BufferedReader br = new BufferedReader(new FileReader(dataFile)); String scoresString = new String(); String[] scoresArray = new String[36]; int[] scoresIntArray = new int[36]; scoresString = br.readLine(); scoresArray = scoresString.split(" "); for(int i = 0; i < scoresArray.length; i++) { scoresIntArray[i] = Integer.parseInt(scoresArray[i]); } String displayRows = new String(); for(int i = 0; i < scoresIntArray.length; i++) { displayRows += scoresIntArray[i++] + " " + scoresIntArray[i++] + " " + scoresIntArray[i++] + " " + scoresIntArray[i] + "\n"; } System.out.println(displayRows); br.close(); System.out.println("The total of the array is " + ArrayOps1D.getTotal(scoresIntArray) + "."); System.out.println("The average of the array is " + ArrayOps1D.getAverage(scoresIntArray) + "."); System.out.println("The lowest number of the array is " + ArrayOps1D.getLowest(scoresIntArray) + "."); System.out.println("The highest number of the array is " + ArrayOps1D.getHighest(scoresIntArray) + "."); System.out.println("The median of the array is " + ArrayOps1D.getMedian(scoresIntArray) + "."); System.out.println("The position of the number in the array is " + ArrayOps1D.getPosition(scoresIntArray,68)); System.out.println("The position of the number in the array is " + ArrayOps1D.getPosition(scoresIntArray,77)); System.out.println("The position of the number in the array is " + ArrayOps1D.getPosition(scoresIntArray,99)); } }
What I'm having trouble with is when I call the method get position, it needs to find the position of the number you are checking for. Two numbers (68 and 99) work fine because they exist in the scores array, but 77 does not. I'm trying to figure out a way to display a message saying this number does not exist within the array, but am having trouble logically figuring it out. It feels like there is a really simple solution but for some reason I can't figure it out.