简体   繁体   English

缺少行吗? 从.txt文件读取字符串并将其放入数组(java)

[英]Missing line? Reading string from .txt file and putting into arrays (java)

Sorry I still new to Java, basically my end result is asking input of a 'pin code' and that pincode will be inside the text document, but I don't sure how to even set them into arrays here below? 抱歉,我还是Java的新手,基本上我的最终结果是要求输入“ pin码”,并且该pincode将在文本文档中,但是我不确定如何将它们设置为下面的数组? Output of my arrays remain null instead of the text in the document. 我的数组的输出保持为null而不是文档中的文本。 Also the catch is the one coming out. 渔获物也是出来的。

The text document is placed in the same folder as the Java classes, should I make a full path? 文本文档与Java类放在同一个文件夹中,我应该使用完整路径吗?

There are only two items in the text,for example I want each to be in listArray[0] and listArray[1] respectively 文本中只有两个项目,例如,我希望每个项目分别位于listArray [0]和listArray [1]中

if inside the document is literally: 如果文档中的字面意思是:

1111,john doe,5000 /n*enter* 2222,glen johnson,4000 1111,john doe,5000 / n * enter * 2222,glen johnson,4000

and i need the pin numbers in the left to be array[0] and for the names for be array[1] 我需要在左边的引脚号为array [0],名称为array [1]

this was an afterthought didnt think about having multiple accounts 这是事后的想法,没有考虑拥有多个帐户

Here is what I've tried so far: 到目前为止,这是我尝试过的:

public class Confirmation extends ATMrunner {
    public static void pinConfirm() {
        String[] listArray = new String[2];
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\Users\workspace\TellerMachine\src\bankATM\userlist.txt"));
            String line = "";
            while ((line = br.readLine()) != null) {
                for (int i = 0; i <= 1; i++) {
                    listArray[i] = line;
                }
            }
            br.close();
        } catch (IOException ioe) {
            System.out.println("File may be corrupted; please contact admin");
        }
        System.out.println(listArray[0]);
        System.out.println(listArray[1]);
    }
}

Appreciate some help! 感谢一些帮助!

Okay, first things first: 好的,首先要做的是:

The fullpath gives me an invalid escape sequence error. 全路径给我一个无效的转义序列错误。

This is because in Java, in fact many programming languages; 这是因为在Java中,实际上有许多编程语言。 when declaring a String there are some special characters that can be used; 声明字符串时,可以使用一些特殊字符; such as a 'new line' character \\n , which is a single character telling whatever is reading the data of the String, that there should be a new line at that point. 例如“换行”字符\\n ,这是一个告诉正在读取String数据的字符,该位置应该有换行。 You can read more about these 'escape sequence's in this Wikipedia article . 您可以在Wikipedia文章中了解有关这些“转义序列”的更多信息。

When you're declaring your absolute path, you have multiple \\ characters within it, which are what are used to denote these special characters. 声明绝对路径时,其中包含多个\\字符,这些字符用来表示这些特殊字符。 Hence with \\Users , it is seen as you are trying to enter a 'special character' of the value \\U which there is none. 因此,对于\\Users ,可以看到您正在尝试输入值\\U的“特殊字符”,而该字符没有。 To combat this issue; 为了解决这个问题; simply replace all \\ characters with \\\\ . 只需将所有\\字符替换为\\\\

Secondly, in your while (and for) loop when reading the file; 其次,在您的while(和for)循环中读取文件时; each itteration of the while loop you are getting a new line of the file. while循环的每次执行都会得到文件的新行。

(line = br.readLine()) != null

And then after this, you are iterating through your String array, "listArray" and setting each element to the current line. 然后,然后遍历String数组“ listArray”,并将每个元素设置为当前行。

for (int i = 0; i <= 1; i++) {
       listArray[i] = line;
}

The problem that will arise here is, you're going to read the first line of the file, then set the two elements of the array to that line. 这里将出现的问题是,您将要读取文件的第一行,然后将数组的两个元素设置为该行。 Then you're going to read another line of the file; 然后,您将读取文件的另一行; and set all the elements to that line. 并将所有元素设置为该行。 And then... 接着...

To change this, I recommend using a technique such as the following: 要更改此设置,我建议使用如下技术:

int index = 0;
String line = "";
while((line = br.readLine()) != null) {
    listArray[index] = line;
    index++;
}

Now, this while loop will do the same thing as you had in yours, however with this one; 现在,这个while循环将执行与您相同的操作,但是使用此循环; you're also keeping track of the current index of the array. 您还将跟踪数组的当前索引。 Each loop of the while loop will read a line and assign the current index to that, then it will increment the index. while循环的每个循环将读取一行并为其分配当前索引,然后它将递增索引。 The problem with this approach is that if your file contains more than 2 lines, the while loop will run a third time and try to assign an out-of-bounds index on the array. 这种方法的问题在于,如果文件包含多于2行,则while循环将第三次运行,并尝试在数组上分配越界索引。 To change this you could try also checking that the index is within the array bounds before trying to assign it, as follows: 要更改此设置,您还可以尝试在尝试分配索引之前检查索引是否在数组范围内,如下所示:

while((line = br.readLine()) != null && index < listArray.length)

Of course there are better ways of doing all this, but all of this should get your application working properly. 当然,有更好的方法可以完成所有这些操作,但是所有这些可以使您的应用程序正常运行。

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

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