Reputation: 13
String menu = JOptionPane.showInputDialog("Enter 'LOGIN', 'REGISTER', or 'EXIT'");
if (menu.equalsIgnoreCase("login") || menu.equalsIgnoreCase("l"))
{
String username = JOptionPane.showInputDialog ("Please enter your username.");
String pinS = JOptionPane.showInputDialog ("Please enter your PIN.");
/* Want to call other class and pass username and pinS
if (LoginVerification (username, pinS) == 1)
loggedIn = true;
else
JOptionPane.showMessageDialog (null, "Account/PIN not found/doesn't match.");
*/
}
That is a section of the main class called Startup.java This is the other class called LoginVerication.java
package bankbalance;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class LoginVerification
{
public void loginVerification (username, pin)
{
boolean usernameFound = false, pinMatches = false;
try
{
Scanner fileReader = new Scanner (new FileReader ("accounts.txt"));
while (fileReader.hasNextLine() && !usernameFound)
{
usernameFound = fileReader.nextLine().indexOf(username) >= 0; // want username to be passed from other class
}
pinMatches = fileReader.nextLine().indexOf(pin) >= 0; // want pin to be passed from other class
if (usernameFound && pinMatches)
{
return 1; // return 1 to other class
}
else
{
return 0; // return 0 to other class
}
}
catch (FileNotFoundException z)
{
JOptionPane.showMessageDialog (null, "No Accounts found. Please register a new account.");
}
}
}
How do I correctly do this? I want the LoginVerification.java to be called from the Startup.java and then return 1 if the username is found and the pin matches for the username. Thanks!
Upvotes: 0
Views: 82
Reputation: 138
public class LoginVerification {
public static int check(String username, String pin) {
boolean usernameFound = false, pinMatches = false;
try {
Scanner fileReader = new Scanner(new FileReader("accounts.txt"));
while (fileReader.hasNextLine() && !usernameFound) {
usernameFound = fileReader.nextLine().indexOf(username) >= 0; // want username to be passed from other class
}
pinMatches = fileReader.nextLine().indexOf(pin) >= 0; // want pin to be passed from other class
if (usernameFound && pinMatches) {
return 1; // return 1 to other class
}
else {
return 0; // return 0 to other class
}
}
catch (FileNotFoundException z) {
JOptionPane.showMessageDialog(null, "No Accounts found. Please register a new account.");
return 0;
}
}
}
and in the calling code :
if (LoginVerification.check (username, pinS) == 1)
loggedIn = true;
else
JOptionPane.showMessageDialog (null, "Account/PIN not found/doesn't match.");
Upvotes: 1
Reputation: 209004
if (LoginVerification.loginVerfication(username, pinS) == 1)
This should work. You should make your method static
since it doesn't seem to rely on any particular instance.
Upvotes: 0