将整数读入IntegerType数组

时间:2014-02-02 01:42:31

标签: java arrays sorting integer

我应该编写一个程序,从txt数组中的IntegerType文件中读取整数。我已经创建了一个实现我的AnyType接口的IntegerType类,您将在代码中看到。然后,我应该对数组进行排序,并研究在排序过程中进行了多少次比较和交换以研究效率,我知道这是O(N^2)。我在代码中设置了断点,向我显示整数被读入我的String []数字数组。当我尝试将它们添加到我的IntegerType数组时,它会命中while (scan.hasNext())代码行并完全跳过我的for循环以将整数添加到我的数组中。任何人都有任何建议如何解决这个问题?谢谢你的时间。这是我的代码:

我的Sorting课程:

public class Sorting {

    public static void main(String[] args) throws IOException {
        int type, sort;
        Scanner read = new Scanner(System.in);

        //Ask user for data type of input
        System.out.println("Make selection by typing corresponding integer value and pressing Enter.");
        System.out.println("Select type of input:");
        System.out.println("1 = Integer  2 = String");
        type = read.nextInt();

        //Ask user for sorting algorithm desired
        System.out.println("Select sorting algorithm to be used:");
        System.out.println("1 = Insertion  2 = Selection  3 = Bubble");
        sort = read.nextInt();

        //Read in integer values from generated .txt files into corresponding integer arrays
        Scanner scan = new Scanner(new File("descending.txt"));
        String line = scan.nextLine();
        String[] numbers = line.split(" ");
        IntegerType[] worstCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < worstCase.length; i++) {
                worstCase[i] = new IntegerType(scan.nextInt());
            }
        }

        scan = new Scanner(new File("random.txt"));
        line = scan.nextLine();
        numbers = line.split(" ");
        IntegerType[] avgCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < numbers.length; i++) {
                avgCase[i] = new IntegerType(scan.nextInt());
            }
        }

        scan = new Scanner(new File("ascending.txt"));
        line = scan.nextLine();
        numbers = line.split(" ");
        IntegerType[] bestCase = new IntegerType[numbers.length];

        while (scan.hasNext()) {
            for (int i = 0; i < numbers.length; i++) {
                bestCase[i] = new IntegerType(scan.nextInt());
            }
        }

        if ((type == 1 || type == 2) && (sort == 1)) //Insertion Ascending
        {
            System.out.println("Insertion Sort / Ascending / Worst Case");
            Sort.insertionSort(worstCase, worstCase.length);
            System.out.println("Insertion Sort / Ascending / Average Case");
            Sort.insertionSort(avgCase, avgCase.length);
            System.out.println("Insertion Sort / Ascending / Best Case");
            Sort.insertionSort(bestCase, bestCase.length);
        }
    }
}

我的Sort课程:

public class Sort {

    public static void insertionSort(AnyType[] list, int size) {
        int compare = 0, swap = 0;
        AnyType key;

        for (int i = 1; i < size; i++) {
            key = list[i];
            int j = i - 1;
            compare++;

            if ((j > -1) && (list[j].isBetterThan(key))) {
                list[j + 1] = list[j];
                j--;
                swap++;
            }
            list[j + 1] = key;
        }

        System.out.println("There were " + compare + " comparisons made.");
        System.out.println("There were " + swap + " swaps made.");
    }
}

我的AnyType界面

public interface AnyType { 
    public boolean isBetterThan(AnyType datum);
}

我的IntegerType班级

public class IntegerType implements AnyType {

    private int number;

    IntegerType() {
        number = 0;
    }

    IntegerType(int i) {
        number = i;
    }

    IntegerType(String s) {
        number = Integer.parseInt(s);
    }

    public boolean isBetterThan(AnyType datum) {
        return (this.number > ((IntegerType) datum).number);
    }

    public int toInteger() {
        return number;
    }
}

1 个答案:

答案 0 :(得分:0)

我想该文件由一行中的空格分隔整数值组成。 无论如何,你应该使用Scanner.hasNextInt()方法进行检查。 所以问题出在这里(见评论):

    //Read in integer values from generated .txt files into corresponding integer arrays
    Scanner scan = new Scanner(new File("descending.txt"));
    String line = scan.nextLine();     //reads already the whole line
    String[] numbers = line.split(" ");   //now a string array with all the values

    //there is nothing left, everything is already stored in "line"/"numbers" variables
    while (scan.hasNext()) {   
        //something
    }

由于您现在事先没有多少整数值,因此您需要一个IntegerType值的动态数据结构(例如,一个ArrayList)。或者,您遍历数字数组并将每个字符串值转换为int:

    Scanner scan = new Scanner(new File("descending.txt"));
    String line = scan.nextLine();
    String[] numbers = line.split("\\s+");
    IntegerType[] worstCase = new IntegerType[numbers.length];

    for(int i = 0; i < numbers.length; ++i)
        worstCase[i] = new IntegerType(numbers[i]);  //will work because IntegerType has a constructor which accepts a string 
    }

您的Sort方法的另一个注释:它们不需要数组的长度作为参数,您可以使用array.length(但这只是我猜的味道)。