• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Create a Java application to validate the user credential.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, we were assigned to make a program that will check username and password with 3 attempts using a text file, hashmap, io, and exceptions. The thing is I do not know how to start it because I do not understand the topic about hashmap. I hope you guys can help
 
Marshal
Posts: 6209
501
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

It's hard to know where to begin with helping because it's unclear how far you've gotten with it. Perhaps you can show us what you've got so far and what specific problem you are facing?
 
Marshal
Posts: 81615
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You do realise that storing a password is very insecure and nobody does that in reeal life. You also realise that we won't give you a full answer but will help you if you are allowed to show us the code posing you problems. I suggest you find out about maps in the Java™ Tutorials. You may wish to concentrate on the two sections in that link about Maps.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

neo zef wrote:... I do not know how to start it because I do not understand the topic about hashmap...

You could start by telling us how you were planning to use the text file, how you were going to handle the exceptions and what exactly is unclear to you about HashMap. In a nutshell, it's just a key value pair data structure.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even without knowing much about hashmaps, there's a lot you could do.  you know you'll need to ask for a user name, ask for a password, read <something> from a file, and you need a loop that does something three times.  Try and do one piece at a time, get it to work, then add in another piece.  Then show us what you have, and tell us specifically where you are stuck.
 
neo zef
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I dont know if this will be under the replies, this is my first time using Ranch. I already have a text file which is just a simple "user1 pass1" up to 4. I think Ive already made a progress on the boolean part, I just do not know how to make it compatible with a text file or how to even use a text file and a hashmap. currently a first year info system who's pretty bad at programming
boolean.JPG
boolean
boolean
login-credentials.JPG
credentials
credentials
 
Campbell Ritchie
Marshal
Posts: 81615
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please quote the code as text with the code button because screenshots are much more difficult to copy. Also, some people find multicoloured text hard to read.
Don't create new Scanners to read System.in. You never need more than one Scanner to read System.in.
 
neo zef
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.*;

public class Mp3 {
   public static void main (String [] args) throws IOException {
     
       Scanner input = new Scanner(System.in);
       HashMap<String, String> map = new HashMap<>();
       File loginCredentials = new File (args[0]);
       BufferedReader br = new BufferedReader(new FileReader(loginCredentials));

       String str;

       while((str = br.readLine()) != null)
       {
           System.out.println(str);
       }
       br.close();
       
       String username;
       String password;

       while ((username = br.readLine()) !=null) {
           password = br.readLine();
           map.put(username, password);

       
       boolean wrongPass = true;

       for (int passAttempts = 0; passAttempts < 3 && wrongPass; passAttempts++) {
           System.out.print("\nEnter Your Password: ");
           String inputUser = input.nextLine();
           System.out.print("\nEnter Your Password: ");
           String inputPass = input.nextLine();

           if(password.equals(map.get(username))){
               System.out.println("Access Granted");
               break;
           }
           else {
               passAttempts--;
               System.out.println("Invalid Credential/s you only have " + passAttempts + " attempt/s remaining");
               };
               
       }

           
               
           }
       }
   }
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic