显示存储在HashMap中的对象值 [英] Displaying object values which are stored in a HashMap
问题描述
我有一个HashMap,其中存储了一个字符串(键)和一个Student对象(值).我希望遍历哈希并显示每个键/值对.但是,我要显示键,然后显示存储在Student对象中的字符串名称值.我尝试了几种方法,每种方法都以某种方式使我失败.我相信我不完全了解静态"修饰符,也不完全了解如何使用HashMaps.
I have a HashMap in which I have stored a string(key) and a Student object(value). I wish to iterate through the hash and display each key/value pair. However, I want to display the key followed by the String name value stored in the Student object. I have tried several ways and each one fails me in some way. I believe I do not entirely understand the "static" modifier, nor do I fully understand how to use HashMaps.
这是我的完整程序:
import java.io.*;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Iterator;
public class FinalProgram {
public static void main(String[] args) throws IOException {
nameReader();
}
private static void nameReader() throws IOException {
String nameFile = " ";
String next = " ";
int sNumber = 0;
String formatSNumber = " ";
String sName = " ";
Scanner input = new Scanner(System.in);
HashMap<String, Student> map = new HashMap<>();
try {
System.out.print("Enter the Name file(c:filename.txt): ");
nameFile = input.nextLine();
} catch(IllegalArgumentException e) {
System.out.printf("Invalid input. Please enter"
+ " filename in the form of "
+ "c:filename.txt\n", e.getMessage());
}
//Instantiate FileReader and BufferedReader
FileReader freader = new FileReader(nameFile);
BufferedReader Breader = new BufferedReader(freader);
boolean end = Breader.ready();
do {
next = Breader.readLine();
sNumber = Integer.parseInt(next);
formatSNumber = String.format("%03d", sNumber);
sName = Breader.readLine();
Student student = new Student(sName);
map.put(formatSNumber, student);
end = Breader.ready();
} while(end);
Iterator<String> keySetIterator = map.keySet().iterator();
while(keySetIterator.hasNext()) {
String key = keySetIterator.next();
System.out.println("key: " + key + " value: " + map.get(Student.getName()));
}
}
这是我读取名称文件的算法:
This is my algorithm for reading the name file:
//Declaration of HashMap
HashMap<String, Student> map = new HashMap<>();
do {
next = Breader.readLine();
sNumber = Integer.parseInt(next);
formatSNumber = String.format("%03d", sNumber);
sName = Breader.readLine();
Student student = new Student(sName);
map.put(formatSNumber, student);
end = Breader.ready();
} while(end);
Iterator<String> keySetIterator = map.keySet().iterator();
while(keySetIterator.hasNext()) {
String key = keySetIterator.next();
System.out.println("key: " + key + " value: " + map.get(key));
}
这是我的学生类(由于遇到了问题,我将同时进行getter和setts并作成构造函数,
This is my Student class(I have done both getters and setts and made a constructor since I am having issues which I will explain next:
public class Student {
private String name = " ";
public Student(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
Course course = new Course();
}
现在,当我尝试使用以下方法遍历地图时:
Now, when I try to iterate through my map with:
while(keySetIterator.hasNext()) {
String key = keySetIterator.next();
System.out.println("key: " + key + " value: " + map.get(key));
}
输出为:
Enter the Name file(c:filename.txt): c:name.txt
key: 001 value: finalprogram.Student@5cbc508c
key: 002 value: finalprogram.Student@3419866c
key: 030 value: finalprogram.Student@63e31ee
它存储了完美的键,但是我无法显示它来显示Student对象的名称.我也尝试过静态变量,但是它返回null(我向Student类中的每个变量和方法都添加了"static"):
It stores the keys perfect, but I cannot get it to display the name of the Student object. I have tried static variables as well, but it returns null(I added 'static' to every variable and method in the Student class):
Enter the Name file(c:filename.txt): c:name.txt
key: 001 value: null
key: 002 value: null
key: 030 value: null
当我尝试直接调用它时:
and when I try to call it directly:
System.out.println("key: " + key + " value: " + map.get(Student.getName()));
我收到错误:
Non-static method getName() cannot be referenced rom a static context.
推荐答案
如果您没有为Student类提供toString()实现,它将打印您所看到的默认值.您可以覆盖toString().
If you don't provide a toString() implementation for your Student class, it prints the default which is what you are seeing. You can override toString().
public class Student {
private String name = " ";
public Student(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
Course course = new Course();
public String toString() {
return name;
}
}
与Student.getName()
一样,您正在尝试调用类的方法,而不是类的实例. Student.getName()
是调用静态方法的方式.您想为非静态方法调用类似key.getName()
之类的东西.
As for Student.getName()
, you are attempting to call a method on a class and not an instance of a class. Student.getName()
is how you call a static method. You want to call something like key.getName()
for a non-static method.
但这实际上不是您的问题.正确的语法应为map.get(key).getName()
;
But that's not actually your problem. The correct syntax would be map.get(key).getName()
;
这篇关于显示存储在HashMap中的对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!