Java programming
$begingroup$
I have been working on this program a while trying to fix this problem, but with no success. So I ask for some assistance here. This java program gives the error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NetworkandInternetSecurity.Login.main(Login.java:113)
Register.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 Register {
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);
// store usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while(sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next();
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.println("Enter new user, Yes or No?");
String response = sc.next();
while (response.equals("Yes")) {
System.out.print("choose 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("rn");
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.close();
System.out.println("Do you want to create another user, Yes or No?");
response = sc.next();
}
System.out.println("Account created.");
}
}
Login.java
package Networtk;
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 Login {
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 the program first. ");
System.exit(0);
}
ArrayList <String> user_ar = new ArrayList<String>();
ArrayList <String> pass_ar = new ArrayList<String>();
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 = 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();
System.out.print("Enter your password: ");
String pwdword = sc.next();
int index=user_ar.indexOf(userName);
if (index!=-1) {
if (hash(pwdword).equals(pass_ar.get(index))) {
done = true;
System.out.println("Login successful");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("Login unsuccessful. You are not registered.");
}
attempts++;
if (attempts == 3 && !done) {
System.out.println("You reached the max login attempts. Account locked..");
done = true;
}
}
}
}
java object-oriented security
New contributor
$endgroup$
add a comment |
$begingroup$
I have been working on this program a while trying to fix this problem, but with no success. So I ask for some assistance here. This java program gives the error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NetworkandInternetSecurity.Login.main(Login.java:113)
Register.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 Register {
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);
// store usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while(sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next();
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.println("Enter new user, Yes or No?");
String response = sc.next();
while (response.equals("Yes")) {
System.out.print("choose 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("rn");
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.close();
System.out.println("Do you want to create another user, Yes or No?");
response = sc.next();
}
System.out.println("Account created.");
}
}
Login.java
package Networtk;
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 Login {
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 the program first. ");
System.exit(0);
}
ArrayList <String> user_ar = new ArrayList<String>();
ArrayList <String> pass_ar = new ArrayList<String>();
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 = 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();
System.out.print("Enter your password: ");
String pwdword = sc.next();
int index=user_ar.indexOf(userName);
if (index!=-1) {
if (hash(pwdword).equals(pass_ar.get(index))) {
done = true;
System.out.println("Login successful");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("Login unsuccessful. You are not registered.");
}
attempts++;
if (attempts == 3 && !done) {
System.out.println("You reached the max login attempts. Account locked..");
done = true;
}
}
}
}
java object-oriented security
New contributor
$endgroup$
add a comment |
$begingroup$
I have been working on this program a while trying to fix this problem, but with no success. So I ask for some assistance here. This java program gives the error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NetworkandInternetSecurity.Login.main(Login.java:113)
Register.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 Register {
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);
// store usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while(sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next();
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.println("Enter new user, Yes or No?");
String response = sc.next();
while (response.equals("Yes")) {
System.out.print("choose 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("rn");
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.close();
System.out.println("Do you want to create another user, Yes or No?");
response = sc.next();
}
System.out.println("Account created.");
}
}
Login.java
package Networtk;
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 Login {
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 the program first. ");
System.exit(0);
}
ArrayList <String> user_ar = new ArrayList<String>();
ArrayList <String> pass_ar = new ArrayList<String>();
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 = 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();
System.out.print("Enter your password: ");
String pwdword = sc.next();
int index=user_ar.indexOf(userName);
if (index!=-1) {
if (hash(pwdword).equals(pass_ar.get(index))) {
done = true;
System.out.println("Login successful");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("Login unsuccessful. You are not registered.");
}
attempts++;
if (attempts == 3 && !done) {
System.out.println("You reached the max login attempts. Account locked..");
done = true;
}
}
}
}
java object-oriented security
New contributor
$endgroup$
I have been working on this program a while trying to fix this problem, but with no success. So I ask for some assistance here. This java program gives the error:
Exception in thread "main"
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NetworkandInternetSecurity.Login.main(Login.java:113)
Register.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 Register {
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);
// store usernames
ArrayList user_ar = new ArrayList();
ArrayList pass_ar = new ArrayList();
while(sc1.hasNext()) {
String user1 = sc1.next().trim();
String hash1 = sc1.next();
user_ar.add(user1);
pass_ar.add(hash1);
}
// register
Scanner sc = new Scanner(System.in);
System.out.println("Enter new user, Yes or No?");
String response = sc.next();
while (response.equals("Yes")) {
System.out.print("choose 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("rn");
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.close();
System.out.println("Do you want to create another user, Yes or No?");
response = sc.next();
}
System.out.println("Account created.");
}
}
Login.java
package Networtk;
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 Login {
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 the program first. ");
System.exit(0);
}
ArrayList <String> user_ar = new ArrayList<String>();
ArrayList <String> pass_ar = new ArrayList<String>();
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 = 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();
System.out.print("Enter your password: ");
String pwdword = sc.next();
int index=user_ar.indexOf(userName);
if (index!=-1) {
if (hash(pwdword).equals(pass_ar.get(index))) {
done = true;
System.out.println("Login successful");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("Login unsuccessful. You are not registered.");
}
attempts++;
if (attempts == 3 && !done) {
System.out.println("You reached the max login attempts. Account locked..");
done = true;
}
}
}
}
java object-oriented security
java object-oriented security
New contributor
New contributor
New contributor
asked 56 mins ago
Keith Harris Jr.Keith Harris Jr.
11
11
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Keith Harris Jr. is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216204%2fjava-programming%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Keith Harris Jr. is a new contributor. Be nice, and check out our Code of Conduct.
Keith Harris Jr. is a new contributor. Be nice, and check out our Code of Conduct.
Keith Harris Jr. is a new contributor. Be nice, and check out our Code of Conduct.
Keith Harris Jr. is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216204%2fjava-programming%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown