-1

I MAY have found a solution: InputStream will not reset to beginning HOW can i implement this into my code? I need to read a text file more than once using:

while (true){
final String checkUsername = brUsername.readLine();

if (checkUsername == null) {

break;
}
if (checkUsername.equals(usernameInput)) {
correctUsername = true;
}
}

The readers and writers:

FileWriter fwUsername = new FileWriter("Username.txt", true);
FileReader frUsername = new FileReader("Username.txt");// reads the created file Username.txt
PrintWriter pwUsername = new PrintWriter(fwUsername, true);
BufferedReader brUsername = new BufferedReader(frUsername);

I can't do this while loop more than once. NO ERROR is given but the while loop is SKIPPED. 1. How can I repeat this loop? 2. If I can't repeat this loop, how can i store all of its values so i can check it like that? 3. This link may have a solution but i don't know how to do the answer: How to reopen a file from a input stream. I am a beginner so I don't have much knowledge. The code i am using: dropbox.com/s/du1u01f27t0ok2o/codehelp.txt?dl=0

Community
  • 1
  • 1
lolftw
  • 5
  • 4
  • It would be nice to add what language you want do this with. – Bas Oct 04 '14 at 10:07
  • You could try `frUsername.reset()` after reading the file to reset the reading position to the beginning of the file (or to the last set `mark`, but I guess there is none). – Tom Oct 04 '14 at 10:36

1 Answers1

0
frUsername = new FileReader("Username.txt");
brUsername = new BufferedReader(frUsername);
while (true){
  final String checkUsername = brUsername.readLine();
  if (checkUsername == null) {break;}
  if (checkUsername.equals(usernameInput)) {correctUsername = true;}
}
lolftw
  • 5
  • 4
Adrian May
  • 2,127
  • 15
  • 24
  • I think the text file gets read from top to bottom until checkUsername == null. Then when i try and read it again checkUsername still equals null so then it breaks the loop. – lolftw Oct 04 '14 at 10:26
  • So do you mean that you get one good iteration and then null, or is it null right from the start? If the former, can you post what checkUsername contains on the first go round? Either way, can we see the contents of the input file? Is frUsername null? – Adrian May Oct 04 '14 at 10:40
  • But still you shouldn't be calling equals() on something you know to be null. – Adrian May Oct 04 '14 at 10:41
  • It is not null from the start, the loop works once and only after then it is null. I ask the user to enter a username stored in usernameInput above, then the loop checks if that username matches ANY line in the text file. If there is a match correctUsername = true; as seen above. If the username doesnt match I ask them to enter it again and ONLY THEN does the program not check it against the text file because checkUsername already is equal to null. The Username.txt file contains different USERNAMES that have been previously entered on the program stored one after another on separate lines. – lolftw Oct 04 '14 at 10:51
  • Have you looked in the file and checked that they really are on separate lines. If the writer had put them all on the same line it would explain the symptoms. – Adrian May Oct 04 '14 at 11:04
  • Sorry! I was confused by your indentation. You probably want a break after correctUsername=true BTW. – Adrian May Oct 04 '14 at 11:07
  • OK to see the full program follow this link, it may be very stupid, confusing or hard to understand but i would be grateful for help :) : https://www.dropbox.com/s/du1u01f27t0ok2o/codehelp.txt?dl=0 – lolftw Oct 04 '14 at 11:25
  • I have a theory that you're getting the whole file on the first iteration. You can check that by logging checkUsername on that first iteration. – Adrian May Oct 04 '14 at 11:37
  • I think i have found a solution: [InputStream will not reset to beginning](http://stackoverflow.com/questions/6716777/inputstream-will-not-reset-to-beginning) how can i use this in my code? – lolftw Oct 04 '14 at 12:42
  • Well if the problem is that the BufferedReader is stuck at the end I think you'd fix it by making a new one inside the loop every time. You might even have to re-make the FileReader, but hopefully not. I wouldn't screw around with mark. That's for more complicated scenarios and it looks horribly stateful. – Adrian May Oct 04 '14 at 12:52
  • What must i do in terms of flushing and closing the text file? if you look at my code it is all in one big while loop . where and how do i make the new readers – lolftw Oct 04 '14 at 14:41
  • I think i may need to remake the file reader it doesn't seem to be working – lolftw Oct 04 '14 at 14:48
  • I tried it and now it doesn't seem to be working at all now even the first time. i couldn't put BufferedReader brUsername = new BufferedReader(frUsername); because it says its is already defined so i put brUsername = new BufferedReader(frUsername); When must i use .close() and .flush() ? – lolftw Oct 04 '14 at 15:20
  • I have noticed something, when i fail to enter the correct username and password 3 times and i ask them to create a new account (as you can see in the link to the code i posted above) When they create the new username and password it works again but only for the first time. So im not sure why when i re-enter the loop it works again – lolftw Oct 04 '14 at 15:31