How to create an array from the contents of a text file to be sorted?

4066 views java
8

This is a program that takes these three arrays and sorts them using insertion sort and counting the number of comparisons and swaps performed for each array when sorted.

I'm now trying to test three other arrays that have been made on text files. The these three text files are just lists of numbers, the first text file is called "array4.txt" and its list of numbers contains 1 through 2000 in order.

The second file is called "array5.txt" and its list of numbers contains 2000 through 1 in descending order. Lastly, the third file is called "array6.txt" and its list of numbers contains a list of randomly mixed numbers from 1 to 2000, including 1 and 2000 with no repeats.

My goal is to read these files and put their values into an actual array and for my insertion sort method to read them, sort them, and count the number of comparisons and exchanges just as what I did with my first three arrays.

I'm very new to Java and don't know exactly how to do this.

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

public class InsertionSort
{
    public static void main(String args[]) throws IOException
    {    
    int[] Array  = {1,2,3,4,5,6,7,8,9,10};  
    int[] Array2 = {10,9,8,7,6,5,4,3,2,1};
    int[] Array3 = {1,10,2,9,3,8,4,7,5,6};

    System.out.println("Insertion Sort: ");
    System.out.println();
    System.out.println("Best Case Scenario: ");
    printArray(Array);
    insertionSort(Array);

    System.out.println("Worst Case Scenario: ");

    printArray(Array2);

    insertionSort(Array2);

    System.out.println("Average Case Scenario: ");
    printArray(Array3);
    insertionSort(Array3);
}

public static void insertionSort(int[] list) 
{
    int comps = 0, swaps = 0;

    for(int i = 1; i < list .length; i++) {

        int j = i;      

        // compare i with sorted elements and insert it
        // sorted elements: [0..i-1]
        while (j > 0 && list[j] < list[j - 1]) {

            int temp = list[j];
            list[j] = list[j - 1];
            list[j - 1] = temp;

            swaps++;
            comps++;  // loop condition true

            j--;
        }
        comps++; // checking loop condition when false
    }
    //printArray(list);

    System.out.println("Comparisons: " + comps 
        + " Swaps: " + swaps);
    System.out.println();
}

static void printArray(int[] array){

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

}

}

answered question

1 Answer

2

Here you go:

public void getIt() {
    List<Integer> ints = new ArrayList(); //temporary holder

    try (Scanner scanner = new Scanner("filename.txt")) { //open a scanner that will scan our file
        scanner.forEachRemaining(line -> { //iterate through each line in our file
            String[] numberStrings = line.split(","); // the comma is your presumed delimeter IF one exists
            for (int x = 0; x < numberStrings.length; x++) { // loop through each item separated by a comma on each line
                ints.add(Integer.parseInt(numberStrings[x])); // turn this string into an int and add it to your list
            }
        });
    }
    Integer[] integerArray = ints.toArray(new Integer[ints.size()]); //transform our list into an array
}

If it's only one number per line you don't need the for loop or the line.split inside of the forEachRemaining

posted this

Have an answer?

JD

Please login first before posting an answer.