anonymous
2013-01-15 05:53:09 UTC
Problem 1: I have written the code for what I am trying to do but it seems that only the last line of my text file is being added to the HashMap ("amacdonald=amac").
Problem 2: In my text file I have typed out the lines like this(pulled from the login.txt file):
jsmith=12345
sjones=mydog
bprince=mycat
bbarber=1234
amacdonald=amac
When I print out the HashMap, I get: null=jsmith=12345. Would I be able just to add the jsmith = 12345 on its own without the null?
My Code:
****************************************************************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import javax.swing.JOptionPane;
/**
*
* @author @since
*
* Description:
*/
public class Practice3 {
public static void main(String[] args) throws IOException {
boolean done = false;
/*
* Provide a menu to the user to allow them to invoke functions from the
* program.
*/
//Creating the HashMap to import all the users into
HashMap login = new HashMap();
while (!done) {
String optionChosen = JOptionPane.showInputDialog("Enter option to perform:\n 1=Load usernames and passwords from file\n 2=Prompt user for login\n 3=Show details for logins\n 4=Usernames not used\n x=EXIT");
if (optionChosen.toLowerCase().equals("x")) {
done = true;
}
else if (optionChosen.toLowerCase().equals("1")) {
//This is where we will read the file and import all the login information
File file = new File("c:/Practice3/login.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
login.put(null, text);
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
// Displaying the message
JOptionPane.showMessageDialog(null, "File Imported Successfully");
//Displaying the account
System.out.println(login);
}
}
}
}
*********************************************************************************
Thank You for the help in advance I really appreciate it!