I am doing simple exercises on Java loops, but I'm stuck on one.
Goal: system asks username, then password. If their id's correspond, everything is OK. If not, then not.
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
// Suppose we have a database composed of two fields or columns (arrays), the first field is for the username (user[]) and the other one is for the password(pass[]) .
// This is how it looks like:
// user[0] = “Hassan” ;
// user[1] =”Idris”;
// user[2]=”Trevor” ;
// And their passwords correspond with their indexes.
// pass[0] = “homecomingking”;
// pass[1] = “turnupcharlie”;
// pass[2] = “afraidofthedark”;
// Then if one of them had successfully login, the output should be:
// Enter username: Hassan
// Enter password: homecomingking
// Hello Hassan!
// But if not, “Incorrect Login!”
// You can ignore case for the username but not for the password.
// ============= SAMPLE RUN ================
// Program Starts:
// Enter username:
// hassan
// Enter password:
// homecomingking
// Output:
// Hello Hassan!
My code looks like this:
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner person = new Scanner(System.in);
System.out.println("Enter username: ");
String username = person.nextLine();
System.out.println(username);
username==user; // this is not right, i guess
System.out.println("Enter password: ");
String password = person.nextLine();
System.out.println(password);
password==pass; // this is not right as well
if (user.getId.equals(pass.getId)){
System.ot.println("Hello " user);
} else {
System.out.println("Incorrect Login!");
}
I can't understand how to get user id and pass id correspond when asked.
CodePudding user response:
Use compareTo
method of String
to match two strings.
import java.util.*;
class MainTest{
public static void main(String ... $){
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner person = new Scanner(System.in);
System.out.println("Enter username: ");
String username = person.nextLine();
System.out.println(username);
//username==user; // this is not right, i guess
System.out.println("Enter password: ");
String password = person.nextLine();
System.out.println(password);
//password==pass; // this is not right as well
//if (user.getId.equals(pass.getId)){
// System.out.println("Hello " user);
//} else {
// System.out.println("Incorrect Login!");
//}
boolean passwordMatch = false;
for(int i =0 ;i<user.length;i )
if(user[i].compareTo(username) == 0)//checking weather entered username exists or not
if(pass[i].compareTo(password) == 0)//comparing password for corresponding user
passwordMatch = true;//password matched
if(passwordMatch)
System.out.println("Hello \"" username "\"");
else
System.out.println("Incorrect Login!");#password didn't matched
}
}
Output:
$ javac MainTest.java && java MainTest
Enter username:
Hassan
Hassan
Enter password:
homecomingking
homecomingking
Hello "Hassan"
CodePudding user response:
username==user; // this is not right, I guess
First off, this is a comparison which result (true/false) is not stored in any variable. Also, "user" and "pass" are ARRAYS of Strings, which means that they need to be indexed in order to retrieve the actual String you may want to compare it to. What you're trying to find is the index of the inputted username in its array, and compare that to the index of the inputted password in its array, since they are supposed to correlate. It could look something like this:
int userIndex = 0;
for(int i = 0; i < user.length; i )
if(username.equalsIgnoreCase(user[i])
userIndex = i;
int passIndex = 1;
for(int i = 0; i < pass.length; i )
if(password.equals(user[i])
passIndex = i;
if (userIndex == passIndex){
System.out.println("Hello " user[userIndex]);
} else {
System.out.println("Incorrect Login!");
}
However, this is a very overly complicated solution and you're probably better off using a Map-structure for this sort of look-up.
CodePudding user response:
100% working code. 100/100 score for you.
package com.company.testjava;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter username: ");
String username = scanner.nextLine();
System.out.println("Enter password: ");
String password = scanner.nextLine();
int userIndex = -1;
for (int i = 0; i < user.length; i ) {
if (user[i].equals(username)) {
userIndex = i;
break;
}
}
if (userIndex >= 0 && pass[userIndex].equals(password)) {
System.out.println("Hello " username "!");
} else {
System.out.println("Incorrect Login!");
}
}
}