Help sorting array from text file using methods
Hello, I am an extreme novice at Java. I am trying writing a program that would read decimal numbers from a text file, convert the numbers into an array, and then sort the array in ascending order. We are required to use three methods: one method that would get the file and convert the file's contents into an array, one method that would sort the array, and one method to print the output of the sorted array. I seem to be having trouble with my methods as I keep getting these errors when I try to compile my program:
Sorting.java:10: '.class' expected
SelectionSort(double[] numbers);
^
Sorting.java:10: ')' expected
SelectionSort(double[] numbers);
Here is my code:
import java.io.*;
import javax.swing.JOptionPane;
publicclass Sorting
{
publicstaticvoid main (String[] args)
{
ArrayInfo();
SelectionSort(double[] numbers);
PrintSort();
}
publicstaticvoid ArrayInfo()
{
FileReader freader;
BufferedReader inputFile;
String file;
double numbers[];
String str;
int index = 0;
file = JOptionPane.showInputDialog("Please enter the name of a text file.");
try
{
freader =new FileReader(file);
inputFile =new BufferedReader(freader);
str = inputFile.readLine();
while (str !=null && index < numbers.length)
{
numbers[index] = Double.parseDouble(str);
index++;
str = inputFile.readLine();
}
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"The file does not exist.");
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"I/O error");
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Non-numeric data found within the file. The invalid record will be skipped");
}
}
publicstaticvoid SelectionSort(double[]numbers,int size)
{
int startScan;
int index;
int minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = numbers[startScan];
for(index = startScan + 1; index < size; index++)
{
if (numbers[index] < minValue)
{
minValue = numbers[index];
minIndex = index;
}
}
numbers[minIndex] = numbers[startScan];
numbers[startScan] = minValue;
}
}
publicstaticvoid PrintSort()
{
SelectionSort(numbers);
System.out.println("\nThe sorted numbers are: ");
for (int element : numbers)
System.out.print(element +" ");
}
}
Any suggestions to help me solve this problem would be greatly appreciated!
Thanks!
Message was edited by:
theshark

