Reputation: 43
I'm supposed to create a text file with some name and pin number. eg. Peter 1212 John 1234 Mary 0000 these are so called the name and pin number.
write a java program that prompts user for the filepath, name and pin number and checks if its valid.
If correct name and pin, print out "Log in successful" if fail "Log in fail" and if password contain non numerical char, "password contain non-numerical char". Here's what i have so far;
import java.util.*;
import java.io.*;
public class PINCheck {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter file path: ");
String filepath = s.nextLine();
File passwordFile = new File(filepath);
System.out.print("Enter name: ");
String name = s.nextLine();
System.out.print("Enter password: ");
String password = s.nextLine();
if (password.matches(".*[a-zA-Z]+.*")) {
System.out.println("You have entered a non-numerical PIN!");
} else { try {
Scanner sc = new Scanner(passwordFile);
if (sc.hasNext(name) && sc.hasNext(password)) {
System.out.println("You have logged in successfully.");
}else {
System.out.println("Login Failed.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 0
Views: 156
Reputation: 38940
The code should be
import java.util.*;
import java.io.*;
public class PINCheck {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter file path: ");
String filepath = s.nextLine();
File passwordFile = new File(filepath);
System.out.print("Enter name: ");
String name = s.nextLine();
System.out.print("Enter password: ");
String password = s.nextLine();
if (password.matches(".*[a-zA-Z]+.*")) {
System.out.println("You have entered a non-numerical PIN!");
} else {
try {
Scanner sc = new Scanner(passwordFile);
while (sc.hasNext()){
String str = sc.nextLine();
System.out.println("Str="+str);
/*
StringTokenizer st = new StringTokenizer(str);
String uName = st.nextToken();
String uPwd = st.nextToken();
*/
String[] values = str.split(" ");
System.out.println("uname:upwd:"+values[0]+":"+values[1]);
if (name.equals(values[0]) && password.equals(values[1])) {
System.out.println("You have logged in successfully.");
break;
}else {
System.out.println("Login Failed.");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
output:
Enter file path: data.txt
Enter name: ravi
Enter password: 1234
uname:upwd:ravi:1234
You have logged in successfully.
my data.txt file contents : ravi 1234
Note: == compares references of the variable and not content of variables.
You have to compare name & password from file with name & password inputs entered from console. It should be "equals" comparison and it should not be checking references of variables.
Upvotes: 0
Reputation: 41220
Scanner#hasNext(String pattern)
Here String parameter, pattern - a string specifying the pattern to scan, it does not match the value.
What you need iterate over the file content, match username & password with content.
Path path = Paths.get(filepath);
try(Stream<String> lines = Files.lines(path)){
Optional<String> hasUser = lines.filter(s -> s.split(" ")[0].equals(name) && s.split(" ")[1].equals(passowrd)).findFirst();
if(hasUser.isPresent()){
System.out.println("You have logged in successfully.");
}else {
System.out.println("Login Failed.");
}
}
Upvotes: 1