简体   繁体   中英

While-Loop repeating when program reaches else-statement

I'm trying to have this code repeat as long as the user does not type the correct information with the message "incorrect username or password" as you would see on any typical site. When I put the if statement in the loop and run the program, the 'if' statement works fine, however the 'else' statement is spammed. I tried putting the getUsername(); and getPassword(); under 'while(true)' and it seemed to fix that issue, however the 'Enter Username: ' and 'Enter Password: ' is repeated twice. What am I doing wrong?

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Banking_App {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/Users/Coding/Desktop/myFile.txt").toAbsolutePath();
        List<String> titles = Files.lines(path).collect(Collectors.toList());
        String searchUsername = getUsername();
        String searchPassword = getPassword();
        displayResults(searchUsername, searchPassword, titles);
    }

    private static String getUsername() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Username: ");
        return scan.nextLine();
    }

    private static String getPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Password: ");
        return scanner.nextLine();
    }

    public static void displayResults(String searchUsername, String searchPassword, List<String> titles) {
        boolean userInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchUsername));
        boolean passInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchPassword));

        while (true) {
            if (userInFile && passInFile) {
                System.out.println("yes");
            } else {
                System.out.println("Username or Password is Incorrect!");
            }
        }
    }
}

You are looping the results of your display method. You are not looping over the input ie get username and password. The idea is that You get username -> check -> display (These 3 steps happen in 1 big loop). You are looping only over the last step ie display.

package test;

import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;


public class testClass {

    public static void main(String[] args)  {
//        Path path = Paths.get("/Users/Coding/Desktop/myFile.txt").toAbsolutePath();
//        List<String> titles = Files.lines(path).collect(Collectors.toList());
        
        String searchUsername = null;
        String searchPassword =null;
        
        boolean result = false;
        while (!result) {
            searchUsername = getUsername();
            searchPassword = getPassword();
            result = displayResults(searchUsername, searchPassword, null);
            
        }
        System.out.println("break");
        
        
    }

    private static String getUsername() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Username: ");
        return scan.nextLine();
    }

    private static String getPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Password: ");
        return scanner.nextLine();
    }


    public static boolean displayResults(String searchUsername, String searchPassword, List<String> titles) {

//        boolean userInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchUsername));
//        boolean passInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchPassword));
        return true; // false


    }
}

As mentioned by Vishal, you are not looping the inputs.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Banking_App {


    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/Users/Coding/Desktop/myFile.txt").toAbsolutePath();
        List<String> titles = Files.lines(path).collect(Collectors.toList());
        // String searchUsername = getUsername();
        // String searchPassword = getPassword();
        // displayResults(searchUsername, searchPassword, titles);
        displayResults(titles);
    }

    private static String getUsername() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Username: ");
        return scan.nextLine();
    }

    private static String getPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Password: ");
        return scanner.nextLine();
    }


    public static void displayResults(List<String> titles) {
        while (true) {
            String searchUsername = getUsername();
            String searchPassword = getPassword();   
            boolean userInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchUsername));
            boolean passInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchPassword));

            if (userInFile && passInFile) {
                System.out.println("yes");
                break;

            } else {
                System.out.println("Username or Password is Incorrect!");
            }
        }

    }
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Banking_App {


    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/Users/Coding/Desktop/myFile.txt").toAbsolutePath();
        List<String> titles = Files.lines(path).collect(Collectors.toList());
        displayResults(titles);
    }

    private static String getUsername() {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Username: ");
        return scan.nextLine();
    }

    private static String getPassword() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Password: ");
        return scanner.nextLine();
    }


    public static void displayResults(String searchUsername, String searchPassword, List<String> titles) {
        String searchUsername,searchPassword;
        searchUsername = searchPassword = null;
        boolean userInFile,passInFile ;//by default false
        while (true) {
        searchUsername = getUsername();
        searchPassword = getPassword();
        userInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchUsername));
        passInFile = titles.stream().anyMatch(p -> p.equalsIgnoreCase(searchPassword));
            if (userInFile && passInFile) {
                System.out.println("Welcome User");
                break;//essential to break from loop if credentials are correct!!
            } else {
                System.out.println("Username or Password is Incorrect!");
                //will iterate again to take input from user and to check the credentials
            }
        }

    }
}

what you were missing->

1.When username and password is correct break from the loop.

2.If the credentials are wrong again ask for them and check whether they are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM