简体   繁体   English

知道为什么此扫描仪接下来阅读不起作用

[英]Any idea why this scanner read next won't work

I am trying to make an Array that starts at an initial size, can have entries added to it. 我正在尝试制作一个从初始大小开始的数组,可以将条目添加到其中。 (I have to use an Array). (我必须使用一个数组)。 To print the Array I have to following code : 要打印数组,我必须执行以下代码:

public String printDirectory() {
        int x = 0;
        String print = String.format("%-15s" + "%-15s" + "%4s" + "\n", "Surname" , "Initials" , "Number");
//      Sorts the array into alphabetical order
//      Arrays.sort(Array);
        while ( x < count ){
            Scanner sc = new Scanner(Array[x]).useDelimiter("\\t");
            secondName[x] = sc.next();
            initials[x] = sc.next();
            extension[x] = sc.next();
            x++;
        }

        x = 0;
        while ( x < count){
            print += String.format("%-15s" + "%-15s" + "%4S" + "\n", secondName[x] , initials[x] , extension[x]);
            x++;
        }
        return print + "" + Array.length;


    }

Please ignore the extra Array.length, on the return statement. 请忽略return语句上多余的Array.length。

Anyways this is working fine, firstly the Array reads a file which is formated like NameInitialsnumber on each line. 无论如何,这都可以正常工作,首先,数组会读取一个文件,该文件的格式类似于NameInitialsnumber。

So I tried making a newEntry method and it causes problems when I want to print the Array. 所以我尝试制作一个newEntry方法,当我想打印数组时会导致问题。 When I add a new entry, if the Array is too small, it will make the array bigger and add the entry. 当我添加新条目时,如果数组太小,它将使数组变大并添加条目。 I made methods to make sure this worked and it does work. 我制定了一些方法来确保它能够正常工作。 The following code for this method is: 此方法的以下代码是:

public void newEntry(String surname, String in, String ext) {

        if (count == Array.length) {
            String entry = surname + "\t" + in + "\t" + ext;
            int x = Array.length + 1;
            String[] tempArray = new String[x];
            System.arraycopy(Array, 0, tempArray, 0, Array.length);
            Array = tempArray;
            Array[count] = entry;
            Arrays.sort(Array);
        } else {
            String entry = surname + "\t" + in + "\t" + ext;
            Array[count] = entry;
            Arrays.sort(Array);
        }
        count++;
    }

The problem is when I then call the printDirectory method it has problems with sc.next(). 问题是,当我随后调用printDirectory方法时,sc.next()出现了问题。 The error message is as follows: 错误消息如下:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at ArrayDirectory.printDirectory(ArrayDirectory.java:106)
    at ArrayDirectory.main(ArrayDirectory.java:165)

Im really new to coding and im not sure what is wrong. 我对编码真的很陌生,不知道出什么问题了。 I know its something wrong with the new entry but im not sure what. 我知道新条目有问题,但不确定。 Really grateful for any help. 非常感谢您的帮助。 Thanks. 谢谢。

It seems that your other arrays secondName , initials , and extension are not large enough. 看来您的其他数组secondNameinitialsextension不够大。

You need to make them bigger as well. 您还需要使它们更大。 Or even better, when you think a bit about it you will recognize that you do not need them at all. 甚至更好的是,当您对此稍加思考时,您会发现您根本不需要它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM