Question:
Add To a Java HashMap from a Text File?
anonymous
2013-01-15 05:53:09 UTC
I am just starting out with Java and I am at the point where I am trying to add (PUT) items in a HashMap from a text file.

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!
Three answers:
Kaydell
2013-01-15 23:30:52 UTC
The answer that somebody else gave you will work about splitting the lines of text with split() and giving put() two fields. HashMaps do take two values for each entry, a key and a value.



I have something to addd. It's advisable when working with HashMaps and with collections to provide a type so that the compiler can make sure that you're putting in things of the correct type to protect you from runtime crashes which are harder to figure out.



You're HashMap could be declared as in the following link:



http://ideone.com/By6hQd



Specifying types like this are called generic types.



Here's a tutorial on generics:



http://www.tutorialspoint.com/java/java_generics.htm
deonejuan
2013-01-15 06:41:17 UTC
The thing that stands out is:

HashMap

and you are putting in

HashMap



The key can be a String, Integer, Object

the value has the same criteria



So, maybe...



HashMap login = new HashMap();

...

while ((text = reader.readLine()) != null) {

String [] flds = text.split( "=" );



login.put( flds[0], flds[1] );
linaris
2016-08-11 16:16:37 UTC
One process can be to create an array in equal in measurement to the size of the alphabet and use a change-case assertion and a loop to head by way of personality by means of personality and increment the correct slot within the array each time a given letter is determined.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...