Java 将整数读入IntegerType数组

Java 将整数读入IntegerType数组,java,arrays,sorting,integer,Java,Arrays,Sorting,Integer,我应该写一个程序,从IntegerType数组中的txt文件中读取整数。我创建了一个IntegerType类,它实现了您将在代码中看到的AnyType接口。然后我要对数组进行排序,并研究在排序过程中进行了多少次比较和交换,以研究效率,我知道这是O(N^2)。我在代码中设置了断点,这表明整数正在被读入字符串[]数字数组。当我尝试将它们添加到我的IntegerType数组时,它会点击while(scan.hasNext())代码行,并完全跳过for循环将整数添加到数组中。有人对如何解决这个问题有什么

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

我的
排序
类:

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);
        }
    }
}
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;
    }
}

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

由于您现在不预先知道将有多少个整数值,因此需要为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 
    }
Scanner scan=新扫描仪(新文件(“descending.txt”);
String line=scan.nextLine();
字符串[]数字=行。拆分(\\s+);
IntegerType[]worstCase=新IntegerType[numbers.length];
对于(int i=0;i

对于排序方法,还有一点要说明:它们不需要数组的长度作为参数,您可以使用array.length(但我想这只是味道)。

如果您的问题是文件读取循环,您可以通过删除大部分代码并专注于这些代码,极大地简化您的问题。它甚至不需要涉及IntegerType,因为普通整数在这方面是相同的。就我个人而言,我会怀疑
嵌套在
中,而
看起来后者应该是
如果
,或者应该合并到
中,用于
的控件。@keshlam我明白你的意思,但当我尝试时,它仍然给了我一个空数组。我意识到我可以这样做;对于(inti=0;i    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 
    }