我有一个HashMap,我在其中存储了一个字符串(key)和一个Student对象(value)。我希望迭代哈希并显示每个键/值对。但是,我想显示密钥,后跟存储在Student对象中的String name值。我尝试了几种方法,每种方法都以某种方式让我失望。我相信我并不完全理解"静态"修饰符,我也不完全理解如何使用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()));
}
}
这是我读取名称文件的算法:
//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并制作了一个构造函数,因为我遇到了一些问题,我将在下面解释:
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();
}
现在,当我尝试使用:
迭代我的地图时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类的每个变量和方法中添加了&#39; static&#39;)
Enter the Name file(c:filename.txt): c:name.txt
key: 001 value: null
key: 002 value: null
key: 030 value: null
当我尝试直接调用它时:
System.out.println("key: " + key + " value: " + map.get(Student.getName()));
我收到错误:
Non-static method getName() cannot be referenced rom a static context.
答案 0 :(得分:1)
如果您没有为您的Student类提供toString()实现,它会打印出您所看到的默认值。您可以覆盖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()
的内容。
但那不是你的问题。正确的语法是map.get(key).getName()
;
答案 1 :(得分:1)
您没有正确访问Map()
内容。
System.out.println("key: " + key + " value: " + map.get(Student.getName()));
改变是:
System.out.println("key: " + key + " value: " + map.get(key).getName()));
你可以这样想到
Student student = map.get(key);
System.out.println("key: " + key + " value: " + student.getName());
或者,您可以编辑Student()
类,使其在上下文中更加静态,如下所示:
public class Student {
private static String name = " ";
public Student(String name) {
this.name = name;
}
public static void setName(String name) {
this.name = name;
}
public static String getName() {
return name;
}
Course course = new Course();
}
这将消除错误:Non-static method getName() cannot be referenced rom a static context.
但是,我不建议让Student()
更加静态。我建议在系统输出方法之前进行实例化。
最后,您可以在toString()
课程中提供自己的Student()
实施:
public class Student {
private String name = " ";
// Your code here
...
@Override
public String toString() {
return name;
}
}