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

[4936 byte] By [thesharka] at [2007-11-26 22:48:05]
# 1

Well by the way this looks in ArrayInfo() your trying to read a file of doubles into an array called numbers. Then you pass an array called numbers to your selction sort method, but this is NOT the same array. Basicly when you call SelctionSort() your passing it a nothing variable(also your sort metod requires a double [] AND an int), and the information you read in from the Array Info method never leaves that method.

Also you never instantiate your array in the ArrayInfo() method. So when you got this to run you would get NullPointerExceptions.

So the solution can be one of a few things. First you could declare your numbers array globaly ie:import java.io.*;

import javax.swing.JOptionPane;

public class Sorting

{

double[] numbers;

public static void main (String[] args)

{

ArrayInfo();

SelectionSort();

PrintSort();

}

// Rest of your code modified to use the global numbers array

}

Or (I like this way better) Change your methods to return arrays. ie:

import java.io.*;

import javax.swing.JOptionPane;

public class Sorting

{

public static void main (String[] args)

{

double[] numbers;

numbers = ArrayInfo();

numbers = SelectionSort(numbers);

PrintSort(numbers);

}

public static double[] ArrayInfo()

{

// your code that works on the array.

return numbers;

}

public static double[] SelectionSort(double[]numbers)

{

// your code that sorts the array.

return numbers;

}

// the rest of your code.

}

dunnigan14a at 2007-7-10 12:07:31 > top of Java-index,Java Essentials,New To Java...
# 2
Don't doSelectionSort(double[] numbers);but do it like this:SelectionSort(numbers);
prometheuzza at 2007-7-10 12:07:31 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks! The program compiles and it works properly. I still have one question, though. I want to make the array of an undetermined size. It works right now only if I specify the size of the array. Here's what my code looks like now:

public static void main (String[] args)

{

double[] numbers;

numbers = ArrayInfo();

numbers = SelectionSort(numbers);

PrintSort(numbers);

}

public static double[] ArrayInfo()

{

FileReader freader;

BufferedReader inputFile;

String file;

double[] ArrayInfo = new double[7];

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 < ArrayInfo.length)

{

ArrayInfo[index] = Double.parseDouble(str);

index++;

str = inputFile.readLine();

}

}

thesharka at 2007-7-10 12:07:31 > top of Java-index,Java Essentials,New To Java...
# 4
Arrays in Java have a fixed size. The way to go is to learn and use the collections framework: http://java.sun.com/docs/books/tutorial/collections/index.html
DrLaszloJamfa at 2007-7-10 12:07:31 > top of Java-index,Java Essentials,New To Java...