I'm having trouble with another method I'm trying to create. I'm trying to find a position of a number in the array but I previously sorted it in the getMedian method. I thought when I sorted it in the median method, it only stays sorted for the median method. However, whenever I try to find a position of a number in the array it finds it in the sorted array, but I want to find it from the original array.
import java.io.*;
import java.util.Arrays;
public class staticMethods{
public static void main(String[] args) throws IOException{
File dataFile = new File("C:\\Documents and Settings\\Mark\\Desktop\\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();
getTotal(scoresIntArray);
getAverage(scoresIntArray);
getLowest(scoresIntArray);
getHighest(scoresIntArray);
getMedian(scoresIntArray);
getPosition(scoresIntArray, 93);
}
public static void getTotal(int[] scores)
{
int total = 0;
for(int j = 0; j <scores.length; j++)
{
System.out.println(scores[j]);
total = total + scores[j];
}
System.out.println("\nThe total of the array is " + total);
}
public static void getAverage(int[] scores){
int total = 0;
int average = 0;
for(int i = 0; i < scores.length; i++)
{
total += scores[i];
average = (total / scores.length);
}
System.out.println("The average of the array is " + average);
}
public static void getLowest(int[] scores)
{
int lowest = 100;
for(int i = 0; i < scores.length; i++)
{
if (scores[i] < lowest)
{
lowest = scores[i];
}
}
System.out.println("The lowest number in the array is " + lowest);
}
public static void getHighest(int[] scores)
{
int highest = 0;
for(int i = 0; i < scores.length; i++)
{
if(scores[i] > highest)
{
highest = scores[i];
}
}
System.out.println("The highest number in the array is " + highest);
}
public static void 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];
}
System.out.println("The median of the array is " + median);
}
public static void getPosition(int[]scores, int number)
{
for (int i = 0; i < scores.length; i++)
if(scores[i] == number)
System.out.println("The position of the number in the array is " + i);
}
}
How do I "unsort" it back to the original array?