• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hash maps

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a hash map that stores user name and password;

Map<String, byte[]> userData = new HashMap<String, byte[]>();
-this map stores a username of type string and password stoerd as hash value

i can successfully add user (user name& password)

i need help in implementing a getPassword(): this method must ask the user to enter username and should return the associated password..

here is my code; pliz help

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;



public class PasswordService
{
//The hash is to be formed using the SHA algorithm
//to create a MessageDigest
private final String algorithmName = "SHA";

//Use a message digest to create hashed passwords
private MessageDigest md = null;

//We simulate a database of users using who have a login and password
//as a key and value pair in a Map
private Map<String, byte[]> userData;

//complete the constructor
public PasswordService()
{
//intialize the class instance data
userData = new HashMap<String, byte[]>();
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
System.out.println("No Such Algorithm");
e.printStackTrace();
}
}

public void addUser(String login, String password)
{

Scanner in = new Scanner(System.in);

for(;;){
System.out.print ("Enter login name..");
login = in.nextLine();

System.out.print("Password: ");
password = in.nextLine();

byte[] pass = md.digest(password.getBytes());
userData.put(login, pass);

System.out.println("User added successfully..");

Iterator iterator = userData.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
System.out.println("Login: " + key + " Password: " + userData.get(key));
}
}
}

public byte[] getPassword(String login)
{
//TODO
Scanner sc = new Scanner(System.in);

System.out.println("Please enter login name....");
login = sc.nextLine();

// String pass = new String(new byte[]<userData>);
byte[] bt = new <byte[]> userData.get(login);
System.out.println("Password " + bt);
return null;
}
}
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, please put your code between code tags.

What exactly is your problem here ? What error message do you get? Where does your program choke ? Without this information it's hard to help you ...
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


um getting a "userData cannot be resolved as type".. nid help with th getPassword() method
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post a stacktrace ?

Also, this looks weird



A cast is done with (), and you do not instantiate a value that comes from a map, since it alreadt holds the instance.
 
Lesole Mphinyane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx.. i used () to cast n it works perfectly...

thnx! thnx!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This also creates an endless loop:



From what I can gather from your code it looks like it isn't required either.
 
reply
    Bookmark Topic Watch Topic
  • New Topic