Question

3. Use a text editor to create a comma-delimited file of user IDs and passwords. Revise...

3. Use a text editor to create a comma-delimited file of user IDs and passwords. Revise any one of the games you have created throughout this book so the user must first enter a correct ID and its associated password before playing. Save the program as GameWithPassword.java.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Please add code to open your previous game when login is successful. I already highlighted that part,

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class GameWithPassword {

   static int checkLoginDetails(String username, String password) {
       int status = 0; //status 0 means username does not exist
       try {
           //opening file
           BufferedReader br = new BufferedReader(new FileReader("loginDatabase.txt"));
           String line = null;
           //reading line by line
           while ((line = br.readLine()) != null) {
               String[] values = line.split(",");//split username and password
               if (values[0].equals(username)) {//checking for username
                   if (values[1].equals(password)) {//checking for password
                       br.close();
                       return 1;//both are correct
                   } else {
                       status = 2; //username is correct but password is wrong
                   }
               }
           }
           br.close();
       } catch (IOException e) {
           System.out.println("File does not exist.");
           e.printStackTrace();
       }
       return status;
   }

   public static void main(String[] args) {
       //creating instance of scanner class
       Scanner read = new Scanner(System.in);
       String username,password;
       int result;
       //reading username
       System.out.print("Please give username :");
       username=read.nextLine();
       //reading password
       System.out.print("Please give password :");
       password=read.nextLine();
       //Checking login details
       result=checkLoginDetails(username,password);
       //printing appropriate message
       if(result==0){
           System.out.println("Username does not exist.");
       }else if(result==2){
           System.out.println("Password is not correct for username : "+username);
       }else{//login is successful
           System.out.println("Success");
           //ADD CODE TO OPEN GAME
       }
       read.close();
   }

}

Outputs:

Add a comment
Know the answer?
Add Answer to:
3. Use a text editor to create a comma-delimited file of user IDs and passwords. Revise...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT