Use a text editor to create a comma-delmited file of user IDs and passwords. Rev
ID: 3686567 • Letter: U
Question
Use a text editor to create a comma-delmited file of user IDs and passwords. Revise the program below so that the user must first enter a correct ID and its associated passwords before playing.
package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
int computer;
int person;
System.out.println("1 = Rock");
System.out.println("2 = Paper");
System.out.println("3 = Scissors");
computer = ((int)(Math.random() * 100) %
3 + 1);
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number");
person = input.nextInt();
System.out.println("You chose" + person);
System.out.println("The computer chose" + computer);
if(computer == person)
System.out.println("The result is a tie");
else if(computer == 1 & person == 2)
System.out.println("You have won");
else if(computer == 2 & person == 3)
System.out.println("You have won");
else if(computer == 3 & person == 1)
System.out.println("You have won");
else if(computer == 2 & person == 1)
System.out.println("You have lost");
else if(computer == 3 & person == 2)
System.out.println("You have lost");
else if(computer == 1 & person == 3)
System.out.println("You have lost");
}
}
Explanation / Answer
Hi, users.txt file should exist in same folder along with your program code.
Content of users.txt"
userID1,Password1
userId2,password2
............
like that
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
FileReader fr = new FileReader("users.txt");
BufferedReader br = new BufferedReader(fr);
System.out.print("Enter user name: ");
String userName = input.next();
System.out.print("Enter password: ");
String password = input.next();
// reading file and searching for userid
String line;
boolean isVerified = false;
while((line = br.readLine()) != null){
String temp[] = line.split(","); // splitting by comma
if(userName.equals(temp[0].trim()) && password.equals(temp[1].trim())){
isVerified = true;
break;
}
}
br.close();
fr.close();
if(!isVerified){
System.out.println("User name or Password is incorrect");
return;
}
int computer;
int person;
System.out.println("1 = Rock");
System.out.println("2 = Paper");
System.out.println("3 = Scissors");
computer = ((int) (Math.random() * 100) %
3 + 1);
System.out.println("Please enter a number");
person = input.nextInt();
System.out.println("You chose" + person);
System.out.println("The computer chose" + computer);
if (computer == person)
System.out.println("The result is a tie");
else if (computer == 1 & person == 2)
System.out.println("You have won");
else if (computer == 2 & person == 3)
System.out.println("You have won");
else if (computer == 3 & person == 1)
System.out.println("You have won");
else if (computer == 2 & person == 1)
System.out.println("You have lost");
else if (computer == 3 & person == 2)
System.out.println("You have lost");
else if (computer == 1 & person == 3)
System.out.println("You have lost");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.