0

I'm loading a file into a buffer which is loaded into a string buffer. I'm then copying this string buffer into a string. The string prints out as...

Sam

ravon

Ashley

annie

So I want to make an array for this string so I can put the first line and second line at a time into a function that creates a node for a LinkedList that takes a username and password ... ex Username: Sam , Password: ravon. This is then loaded into a LinkedList. All of my functionally is working when it comes to the LinkedList, but I can't seem to split my string into an array.

I thought something like...

String[] userContent = content.split("\n") would place each element of content into userContent[n] string where

userContent[0] = Sam, userContent[1] = ravon and etc. However, this is not the case.

code I'm using - It takes a linked list and file name as arguments

    public static void readUserFile(String fName, LinkedList<dataUser> ll) {
    try {
        File file = new File(fName);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line;

        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }

        LineNumberReader  lnr = new LineNumberReader(new FileReader(new File("userData.txt")));
        lnr.skip(Long.MAX_VALUE);
        lnr.close();
        fileReader.close();
        int realSize = lnr.getLineNumber();
        //String[] userContent = line.split("\n");
        String content = stringBuffer.toString();
        String[] userContent = content.split("/n");

         //this prints nothing, would expect it to print exactly what content prints
        for(int i = 0; i < realSize; i++) {
            System.out.println(userContent[i]);
        }

        /*  this is what I want to load the userContent string into
        //not working
        for(int i = 0; i < realSize; i++) {
            dataUser tempUser = new dataUser(userContent[i],            userContent[i+1]);
            ll.add(tempUser);
            i = i + 1;
        }*/

        //System.out.println(content);  //works and prints the file with new lines
        //System.out.println("Contents of file:");
        //System.out.println(stringBuffer.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
6
  • 1
    java.nio.file.Files.readAllLines is probably the way to go. Commented May 20, 2015 at 20:27
  • 4
    Why don't you just fill an ArrayList of strings while you are reading instead of appending to a StringBuffer? Commented May 20, 2015 at 20:29
  • 1
    Oh, and your problem is that you are splitting by /n instead of \n. Commented May 20, 2015 at 20:31
  • readAllLines will place it into one giant string? I haven't used Java before so I've only played with the LinkedList library so far in java. Commented May 20, 2015 at 20:31
  • Ah that was it RealSkeptic, thanks for noticing the error. I also forgot to add +1 to realSize to account for 0 in the array size. Commented May 20, 2015 at 20:33

2 Answers 2

1

For what it's worth the following will do the same thing your code is doing without creating/splitting a String and reopening a file unnecessarily and also won't thrown an exception if you happen to run into a file with a user but no password.

BufferedReader br = new BufferedReader(new FileReader(fName));
String user;

while ((user = br.readLine()) != null) {
    String pass = br.readLine();
    if (pass == null) {
        System.out.println("Warning: User found with no password: " + user);
        break;
    }
    ll.add(new DataUser(user, pass));
}
Sign up to request clarification or add additional context in comments.

2 Comments

How does this answer the OPs question?
The OP has additional issues, for example the hard coding of "userData.txt" instead of using fName and using i+1 while iterating over an array. All that code is completely unnecessary.
0

now you are only checking for whitespaces,

split("\n")

use %%n for new lines, or you can use %%W which is not alphanumeric characters happy coding

Comments

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.