2 Replies - 3692 Views - Last Post: 20 May 2008 - 04:46 AM Rate Topic: -----

#1 VashFanatic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-May 08

Reading From a File and Sorting into an Array

Posted 19 May 2008 - 08:59 PM

Hello! I'm trying to write a program that reads a list of integers from two different files ("F1.txt" and "F2.txt"). Each file will be looked through, the lists of integers will be sorted into arrays. And then, at the end, both arrays will be merged together into one array and sorted again.

What I am having trouble with is understanding how the list of integers will be sorted into an array after they are read. How would I call the sorting method to sort these files and then put them into an array?

Here is my code thus far for reading the file:
public void sortFile ( String fileName ) throws IOException {
		   File file1 = new File("F1.txt");
		   Scanner inputFile1 = new Scanner(file1);
		   while ( inputFile1.hasNext()) {
			  int numbers1 = inputFile1.nextInt();
		   }
		   inputFile1.close();
		
		   File file2 = new File("F2.txt");
		   Scanner inputFile2 = new Scanner(file2);
		   while ( inputFile2.hasNext()) {
			  int numbers2 = inputFile2.nextInt();
		   }
		   inputFile2.close();
		
		
	} 


This is my selection sort method that I'm going to use to sort the files.
public int selectionSort (  ) {
		int startScan, i, minIndex, minValue, counter = 0;
		
		for ( startScan = 0; startScan < (numbers.length - 1); startScan++) {
			minIndex = startScan;
			minValue = numbers[startScan];
			counter++;
			for ( i = startScan + 1; i < numbers.length; i++ ) {
				if ( numbers[i] < minValue ) {
					minValue = numbers[i];
					minIndex = i;
				}
			}
			numbers[minIndex] = numbers[startScan];
			numbers[startScan] = minValue;
		}
	return counter;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Reading From a File and Sorting into an Array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading From a File and Sorting into an Array

Posted 19 May 2008 - 09:15 PM

public void sortFile ( String fileName ) throws IOException {
		   File file1 = new File("F1.txt");
		   Scanner inputFile1 = new Scanner(file1);
		   while ( inputFile1.hasNext()) {
			  int numbers1 = inputFile1.nextInt();
		   }
		   inputFile1.close();
		
		   File file2 = new File("F2.txt");
		   Scanner inputFile2 = new Scanner(file2);
		   while ( inputFile2.hasNext()) {
			  int numbers2 = inputFile2.nextInt();
		   }
		   inputFile2.close();
		
		
	} 


You are not reading into an array... you define numbers1 and numbers2 as single int and do nothing with them. I mean when you exit your while loop numbers1 and numbers2 do not exist anymore

You are passing a "fileName" to your methhod but never use it

You probably want something like that:


int[] numbers1 = new int[1000];
int[] numbers2 = new int[1000];
int numbers1Size = 0, numbers2Size = 0;
public void sortFile () {
		   File file1 = new File("F1.txt");
		   Scanner inputFile1 = new Scanner(file1);
		   while ( inputFile1.hasNext()) {
			  numbers1[numbers1Size++] = inputFile1.nextInt();
		   }
		   inputFile1.close();
		
		   File file2 = new File("F2.txt");
		   Scanner inputFile2 = new Scanner(file2);
		   while ( inputFile2.hasNext()) {
			  numbers2[numbers2Size++] = inputFile2.nextInt();
		   }
		   inputFile2.close();
		
		
	} 

Was This Post Helpful? 0
  • +
  • -

#3 cutegrrl  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 77
  • Joined: 12-May 08

Re: Reading From a File and Sorting into an Array

Posted 20 May 2008 - 04:46 AM



import java.io.*;
import java.util.*;

public class SortFiles
{
	// Create a single shared BufferedReader for keyboard input
	static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String [] args) throws IOException{

		System.out.print("Enter filename of file #1 => ");
		String input = kb.readLine();
		int[] array1 = readFile(input);

		System.out.print("Enter filename of file #2 => ");
		input = kb.readLine();
		int[] array2 = readFile(input);

		// Combine arrays
		int[] array3 = new int[array1.length + array2.length];
		for(int i = 0, j = 0; i < array3.length; i++){
			if(i < array1.length){ // copy contents of array #1
				array3[i] = array1[i];
			}else{ // copy contents of array #2
				array3[i] = array2[j];
				j++;
			}
		}
		
		// Apply selection sort
		array3 = selectionSort(array3);

		// Output sorted array
		for(int i = 0; i < array3.length; i++){
			System.out.print(array3[i] + " ");
		}

	} 

	public static int[] readFile(String filename) throws IOException{

		// Create Reader to read the contents of file
		LineNumberReader in = new LineNumberReader(new FileReader(filename));

		// Read file and store lines in ArrayList (dynamic array)
		ArrayList<Integer> list = new ArrayList<Integer>();
		String str;
		while((str = in.readLine()) != null) {
			try{
				list.add(Integer.parseInt(str));
			}catch(NumberFormatException e){
				System.out.println("Unable to parse line " + in.getLineNumber() + " in \"" + filename + "\"");
			}
		}

		// Copy contents of list to conventional array
		int[] array = new int[list.size()];
		for(int i = 0; i < list.size(); i++){
			array[i] = list.get(i);
		}
		return array;
	}

	public static int[] selectionSort(int[] array){
		for (int i = 1 ; i < array.length ; i++) {

			// Find the index of the ith smallest value
			int s = i-1;
			for (int j = i ; j < array.length ; j++) {
				if (array[j] < array[s]) {
					s = j;
				}
			}

			// Swap the ith smallest value into entry i-1
			int temp = array[i-1];
			array[i-1] = array[s];
			array[s] = temp;

		}
		return array;
	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1