1

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!

4
  • Are you saying you get an IOException ioe? WHat message does it come with? Do you need to fully path the file? Commented Sep 17, 2014 at 9:00
  • Try making it fully pathed and see what happens. Try printing out the exception message if you catch one, to see what it says too. Commented Sep 17, 2014 at 9:08
  • Every time you read a line, you will set all the elements of your array to this value, this is maybe not what you want. Commented Sep 17, 2014 at 9:11
  • 1
    Can you provide content of your txt file? And if you're writing a path with back-slashes write it like this new FileReader("D:\\development\\testing\\userlist.txt") Commented Sep 17, 2014 at 9:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to make it like if it's in a line " john doe, 5000 " where john doe would be the 1st array? and if i were to add a second line just like that? in the end it should be "1111,john doe, 5000" if input of pin is 1111 then it execute the menu i make
Something you can try for that is the String.split(String) method. Once you have your String, "john doe, 5000" you can split it on the comma, like so: String[] parts = line.split(","). This will give you (in this case) an array of 2 Strings, being "john doe" and "5000". Something you should watch out for however, if your going to convert the String "5000" to an Integer later, is if you have a space after the comma. If so, split on ", ". Otherwise you will end up with " 5000".
never used that before, may i ask where its best to put that method?
Use it where you're reading / parsing the lines of the text file. It does depend on how you want to structure your data, but generally speaking it should probably reside within your while loop, before the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.