简体   繁体   中英

java class for reading TXT into hashmap

I've been stuck on this project where it asks me create a class to read multiple txt documents and display them on my main app's textarea.

the documents are in this general format id<>name

The <> needs to be split, and only the name should be displayed in the textarea. My instructor said hash maps would be a good collection to use, but this entire concept is kinda blurry to me.

I need help creating a class that handles I/O + hashmap to store all files, then be able to display the name part for the main app. Help is much appreciated!

Use BufferedReader to read each line from the file and then follow the below pseudo code (Since this is homework I can't provide the actual code). You basically looking for String.split(...) , BufferedReader , FileReader , Map (HashMap) classes.

Step 1

//Read each file in to Map
for each line
{
  split the line at <>
  you will have two tokens
  token 1 is id and token 2 is the name
  store both the tokens in Map (token 1 is the key and token 2 is the value)
}

Step 2

//Display each entry from the map
for each entry in the Map
display the value in text area

As this is a homework, I can only provide some direction to solve the problems.

1) You have multiple documents all containing multiple line with each line having format id<>name.

2) You can read file using Java File I/O API

3) Create hashmap

4) Read each document file line by line

4) Split the line using String.split("<>"), you will get two strings id and name

5) use ID as a key and name as a value. Put this in hashmap

6) After parsing all documents you will have filled hashmap

7) Use java swing API for TextArea to display content hashmap in TextArea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM