Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created June 18, 2018 02:30
Show Gist options
  • Save charsyam/3b0f83ac5af050c7b27c78b7f8bb9786 to your computer and use it in GitHub Desktop.
Save charsyam/3b0f83ac5af050c7b27c78b7f8bb9786 to your computer and use it in GitHub Desktop.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Scanner;
class User {
private String name;
private String email;
private String phone;
private int age;
public User(String name,
String email,
String phone,
int age) {
this.name = name;
this.email = email;
this.phone = phone;
this.age = age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public int getAge() {
return age;
}
public String toString() {
return String.format("%s|%s|%s|%d",
getName(),
getEmail(),
getPhone(),
getAge());
}
}
public class Test2 {
public static User createUser(String line) {
String [] parts = line.split(" ");
int age = Integer.valueOf(parts[3]);
User user = new User(parts[0], parts[1], parts[2], age);
return user;
}
public static void main(String...args) {
String path = args[0];
Map<String, User> map = new HashMap<>();
try {
FileReader fileReader = new FileReader(path);
BufferedReader br = new BufferedReader(fileReader);
String line = null;
while ((line = br.readLine()) != null) {
User user = createUser(line);
map.put(user.getName(), user);
System.out.println(user);
}
fileReader.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
User u1 = map.get(name);
System.out.println(u1);
for (Map.Entry<String, User> entry : map.entrySet()) {
System.out.println("K : " + entry.getKey());
System.out.println("V : " + entry.getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment