Java coding, please help. I have two files, one that allow users to register, and the other file that authenticates them. Problem is i keep getting this error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at network.Register.main(Register.java:56)
At first glance the program seems to work, but here is the steps on how to reproduce the problem. I register a few users, everything goes fine. I authenticate them by login, everything is ok. Now when I go back to try to register more users, this is when the problem appears and the program refuses to run, giving the error stated above. Source codes and input file is below:
Here is what the program printed to the database file before it refused to keep running:
Ryan
8b0e488bdc218700c3018496e0f7efb17c79f415ec392b166df3a931984c2c108df2cdfe3c37f52a891fe390d9950f057a107b24b8a72b24c61a84ebf724bfc2
Harris
John
12f9179594c144c95f8119fb7d8755fbcda550423d31fbe26e52231fd85abad0b9e43cae0667c4088c327c6458a9340e9888000bc15e6d9fb4211cc0c6fb065f
Hill
Darius
cba980aab9c459470a789a2915cd458bfb81b0d875d14cae079c92f96f2232ad93ea389a48790ba210cd4d826e3878ab6ea29d4d8387c8addec4321e04700e3c
Dickson
Dr
7a7365f1de2da275b3fb49ee53646b6405e7102f9ce0ee5d4d87c2eebd337ff1c9b60fd225a1d16ceeb97617e4a2c8bd8b7c86f745403508950b7bc059451b7f
college
Test
bf0c4cb323d5631e5e07990b52512d9b57d48497d289d253dec65903cb480024f0bb6b9f7a8b0df53b55f98a89607892425f1ca304290dc3a2c70f4a818e8424
file
// End of database file
Here are the source codes for the program:
CreateUser.java:
package network;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Scanner;
public class CreateUser {
static String hash(String str) {
String hashed = "";
try {
MessageDigest security =
MessageDigest.getInstance("SHA-512");
security.update(str.getBytes(Charset.forName("UTF8")));
// message digest
byte[] bytes = security.digest();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
buffer.append(Integer.toHexString((bytes[i] & 0xFF) |
0x100)
.substring(1, 3));
}
hashed = buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hashed;
}
// Main Method
public static void main(String[] args) throws
FileNotFoundException, IOException
{
// create file
FileWriter object = new FileWriter("database.txt", true);
File store = new File("database.txt");
Scanner sc1 = new Scanner(store);
// read usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while (sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next().trim();
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.print("Enter new user, Yes or No? ");
String response = sc.nextLine();
while (response.equals("Yes") || response.equals("yes") ||
response.equals("Y") || response.equals("y"))
{
System.out.print("\nChoose a username: ");
String user = sc.next();
while (user_ar.indexOf(user) != -1) {
System.out.println("Username already exists. Please try
again:");
user = sc.next();
}
user_ar.add(user);
System.out.print("Choose a password: ");
String pwd = sc.next();
String newHash = hash(pwd);
// write to file
FileWriter object1 = new FileWriter("database.txt", true);
PrintWriter writer = new PrintWriter(object1);
//writer.write("\r\n");
writer.write(user);
writer.write("\t");
writer.write(newHash);
writer.write("\t"); // Assignment requires plaintext password to be
stored
writer.write(pwd); // Assignment requires plaintext password to be
stored
writer.println(); // This adds a new line after a line has been
written to the file
writer.close();
System.out.print("\nDo you want to create another user, Yes or
No? ");
response = sc.next();
}
System.out.println("Account created.");
}
}
// End of this file
Authenticate.java
package network;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Scanner;
public class Authenticate {
static String hash(String str) {
String hashed = "";
try {
MessageDigest security =
MessageDigest.getInstance("SHA-512");
security.update(str.getBytes(Charset.forName("UTF8")));
byte[] bytes = security.digest();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
buffer.append(Integer.toHexString((bytes[i] & 0xFF) |
0x100)
.substring(1, 3));
}
hashed = buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hashed;
}
public static void main(String[] args) throws FileNotFoundException {
File store = new File("database.txt");
if (!store.exists()) {
System.out.println(store + " not found, run CreateUser program first. ");
System.exit(0);
}
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
Scanner sc = new Scanner(store);
while (sc.hasNext()) {
String user = sc.next().trim();
String hash = sc.next().trim();
user_ar.add(user);
pass_ar.add(hash);
sc.next();
}
sc.close();
sc = new Scanner(System.in);
boolean done = false;
int attempts = 0;
while (!done) {
// ask user for username and password
System.out.print("Enter registered username: ");
String userName = sc.next().trim();
System.out.print("Enter your password: ");
String pwdword = sc.next().trim();
int index = user_ar.indexOf(userName);
if (index != -1) {
if (hash(pwdword).equals(pass_ar.get(index))) {
done = true;
System.out.println("\nLogin successful");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("\nLogin unsuccessful. You are not
registered.");
}
attempts++;
if (attempts == 3 && !done) {
System.out.println("\nYou reached the max login attempts. Account locked..");
done = true;
}
}
}
}
You are missing a single line of code in CreateUser.java. Your database file is created in such a way that each record consist of username, followed by password hash followed by password in plain text. But in your CreateUser class, when you open and read the file, you take only username and hash, leaving the password unread, which will be read by the next sc1.next() call as user name. So I have fixed it by adding sc1.next() after each iteration, to read and ignore the password. But I have a question: why are you opening the file, reading the contents into array lists in CreateUser class. There seems to have no use of those two lists anywhere else in that file. Anyway, I’m not removing that, just added the needed code to make your program work fine. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Only attaching CreateUser.java as other file is not modified.
// CreateUser.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Scanner;
public class CreateUser {
static String hash(String str) {
String hashed = "";
try {
MessageDigest security = MessageDigest.getInstance("SHA-512");
security.update(str.getBytes(Charset.forName("UTF8")));
// message digest
byte[] bytes = security.digest();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
buffer.append(Integer.toHexString((bytes[i] & 0xFF) | 0x100)
.substring(1, 3));
}
hashed = buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hashed;
}
// Main Method
public static void main(String[] args) throws FileNotFoundException,
IOException {
// create file
FileWriter object = new FileWriter("database.txt", true);
File store = new File("database.txt");
Scanner sc1 = new Scanner(store);
// read usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while (sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next().trim();
/**
* you are missing this. In your database.txt file, each record
* contain username, hash and the password. You were only reading
* username and hash, so the password field is left unread in first
* loop, which will be read as the username in next loop, causing
* troubles.
*/
sc1.next(); //reading password, ignoring it
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.print("Enter new user, Yes or No? ");
String response = sc.nextLine();
while (response.equals("Yes") || response.equals("yes")
|| response.equals("Y") || response.equals("y")) {
System.out.print("\nChoose a username: ");
String user = sc.next();
while (user_ar.indexOf(user) != -1) {
System.out
.println("Username already exists. Please try again:");
user = sc.next();
}
user_ar.add(user);
System.out.print("Choose a password: ");
String pwd = sc.next();
String newHash = hash(pwd);
// write to file
FileWriter object1 = new FileWriter("database.txt", true);
PrintWriter writer = new PrintWriter(object1);
// writer.write("\r\n");
writer.write(user);
writer.write("\t");
writer.write(newHash);
writer.write("\t"); // Assignment requires plaintext password to be
// stored
writer.write(pwd); // Assignment requires plaintext password to be
// stored
writer.println(); // This adds a new line after a line has been
// written to the file
writer.close();
System.out
.print("\nDo you want to create another user, Yes or No? ");
response = sc.next();
}
System.out.println("Account created.");
}
}
Java coding, please help. I have two files, one that allow users to register, and the...