chrt
chrt

Reputation: 41

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? Output of my arrays remain null instead of the text in the document. 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?

There are only two items in the text,for example I want each to be in listArray[0] and listArray[1] respectively

if inside the document is literally:

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]

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!

Upvotes: 1

Views: 1477

Answers (1)

T-Fowl
T-Fowl

Reputation: 704

Okay, first things first:

The fullpath gives me an invalid escape sequence error.

This is because in Java, in fact many programming languages; 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. You can read more about these 'escape sequence's in this Wikipedia article.

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. To combat this issue; simply replace all \ characters with \\.

Secondly, in your while (and for) loop when reading the file; each itteration of the while loop you are getting a new line of the file.

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

And then after this, you are iterating through your String array, "listArray" and setting each element to the current line.

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; 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. 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. 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.

Upvotes: 2

Related Questions