I keep getting an error at the very end once I enter my first last and password!
ID: 3733205 • Letter: I
Question
I keep getting an error at the very end once I enter my first last and password! Anyone know what the problem is?
**Exception in thread "main" java.util.NoSuchElementException: No line found
Code Starts HERE:
//Austin Deering, Program 2 Assignment, 3/18/2018, Learning how to create usernames and validating psswords.
import java.util.Scanner;
import java.io.File;
import java.io.*;
public class ProgramTwo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TO DO code application logic here
//scanner object
Scanner sc = new Scanner(System.in);
System.out.println("Enter your first name: ");
String fname = sc.nextLine();
System.out.println("Enter your last name: ");
String lname = sc.nextLine();
//appending the first character
String username = lname + fname.charAt(0);
if(username.length() < 5){
//appending second character
username += fname.charAt(1);
}
username = username.toLowerCase();
String password;
while(true){
System.out.println("Enter a password: ");
password = sc.nextLine();
//flag variables for keeping track of all the requirements
int len_flag = 0, upper_flag = 0, lower_flag = 0, digit_flag = 0;
int punct_flag = 0;
//checking length
if(password.length() >= 8)
len_flag = 1;
for(int i = 0; i < password.length(); i++){
//check if it is a upper case
if(Character.isUpperCase(password.charAt(i))){
upper_flag = 1;
}
//check if it is a lower case
if(Character.isLowerCase(password.charAt(i))){
lower_flag = 1;
}
//check if it is a digit
if(Character.isDigit(password.charAt(i))){
digit_flag = 1;
}
int ascii = password.charAt(i);
//check if it is a punctuation
if(ascii >= 33 && ascii <= 47)
punct_flag = 1;
}
//used to keep track if password exists in the file
int file_flag = 0;
try{
File file = new File("/home/This PC/documents/badpasswords.txt");
Scanner scan = new Scanner(file);
//loop till there exists a new line in the file
while (scan.hasNextLine()){
try{
//load the password from file
String temp_password = scan.next();
temp_password = temp_password.toLowerCase();
//compare to check if it matches with the entered password
if(password == temp_password){
file_flag = 1;
}
//System.out.println(temp_password);
}
catch(Exception e){
break;
}
}
}
catch(IOException exception){
System.out.println("exception");
}
//break if all the conditions are met
if(file_flag == 0 && upper_flag == 1 && lower_flag == 1 && digit_flag == 1 && punct_flag == 1){
System.out.println("Successfully set the password!");
System.out.println("Username: "+username);
break;
}
}
}
}
//output