0

I created a class that verifies usernames & passwords in a text file

When debugging my code I noticed that when i used system.out.print in line 20 the item that was outputted was only the last one on my text file. I know that using println would fix it but i was wondering what what causing this? just to expand my knowledge.

i assumed this is what should have printed out

jon:123432luis:358273frogboy156:32423false

but i get this instead

frogboy15632423false

class

mport java.io.File;
import java.util.Scanner;

public class verificaiton {


    public static void verifyLogin(String username, String password) {
        boolean found = false;
        String tempUsername = "";
        String tempPassword = "";
        try {

            Scanner x = new Scanner(new File("src/info.txt"));
            x.useDelimiter("[:\n]");

            while (x.hasNext() && !found) {

                tempUsername = x.next();
                tempPassword = x.next();
                System.out.print(tempUsername + tempPassword + "");
                if (tempUsername.trim().equals(username) && tempPassword.trim().equals(password.trim())) {
                    found = true;
                }

            }
            x.close();
            System.out.println(found);
        }
        catch(Exception e){
            System.out.println("Error");
        }
    }
}

main

public class main {
    public static void main(String[] args){
    verificaiton check = new verificaiton();
    check.verifyLogin("Luis", "32534");

}}

text file

jon:123432
luis:358273
frogboy156:32423
L.Flores
  • 27
  • 4

1 Answers1

2

The lines in your text file are ending with \r\n. The \n is used as a delimiter. So this means that every password ends with a \r and therefore everything is printed on the same line. See https://stackoverflow.com/a/32697185/1095383

For the first iteration your variables are as following.

String tempUsername = "jon";
String tempPassword = "123432\r";
maroswal
  • 96
  • 6