我从一个ArrayList获取内存地址,需要信息 [英] I am getting the memory address from an arraylist, need info
问题描述
我把文本文件,并填写一个ArrayList。对文件进行测试,我打印出来之前,我继续前进。我只能看到存储器地址,而不是从文件中实际信息。有什么简单而明显的可能是我失踪?
公共类TriviaQuestion {私人字符串的球员;
私人字符串类;
私人字符串的问题;
私人字符串的答案;
私人INT得分= 0;/ **
*
* /
公共TriviaQuestion(){
玩家=未知;
类=未知;
问题=未知;
回答=未知;
得分= 0;
}公共TriviaQuestion(串类,弦乐的问题,弦乐答案){}
公共TriviaQuestion(字符串播放器,串类,弦乐的问题,
字符串的回答,诠释得分){
超();
this.player =玩家;
this.category =类别;
this.question =问题;
this.answer =答案;
this.score =得分;
}
/ **
返回:玩家
* /
公共字符串getPlayer(){
返回的球员;
}
/ **
* @参数播放机播放器设定
* /
公共无效setPlayer(字符串球员){
this.player =玩家;
}
/ **
返回:类别
* /
公共字符串的getCategory(){
返回类;
}
/ **
* @参数类别的类别设置
* /
公共无效setCategory(串类){
this.category =类别;
}
/ **
返回:问题
* /
公共字符串getQuestion(){
返回的问题;
}
/ **
* @参数质疑的问题设置
* /
公共无效setQuestion(字符串问题){
this.question =问题;
}
/ **
返回:答案
* /
公共字符串的getAnswer(){
返回的答案;
}
/ **
* @参数回答的答案为设置
* /
公共无效setAnswer(字符串回答){
this.answer =答案;
}
/ **
返回:比分
* /
公众诠释getScore(){
返回分数;
}
/ **
* @参数得分将比分设置
* /
公共无效setScore(INT分数){
this.score =得分;
}}
该测试仪
进口的java.io.File;
进口java.io.IOException异常;
进口的java.util.ArrayList;
进口java.util.Scanner中;
公共类TriviaQuestionTester {/ **
* @参数ARGS
* /
公共静态无效的主要(字串[] args)抛出IOException 文件triviaFile =新的文件(trivia.txt); 扫描仪triviaFile1 =新的扫描仪(triviaFile);
串lastKnownCategory =; ArrayList的< TriviaQuestion> myList中=新的ArrayList< TriviaQuestion>();
而(triviaFile1.hasNextLine()){
串currentLine = triviaFile1.nextLine();
如果(!currentLine.isEmpty()){
TriviaQuestion currentQuestion =新TriviaQuestion(currentLine,currentLine,currentLine); 如果(currentLine.endsWith(?)){
currentQuestion.setCategory(lastKnownCategory);
currentQuestion.setQuestion(currentLine);
currentQuestion.setAnswer(triviaFile1.nextLine());
}其他{
currentQuestion.setCategory(currentLine);
currentQuestion.setQuestion(triviaFile1.nextLine());
currentQuestion.setAnswer(triviaFile1.nextLine());
lastKnownCategory = currentLine;
}
myList.add(currentQuestion);
}
}
triviaFile1.close(); 的System.out.println(myList中);}}
有没有简单的东西,可能明显我失踪?
块引用>是 - 你没有覆盖
的toString
在TriviaQuestion
,所以你得到的默认实现>对象 :
Object类的toString方法返回一个包含其中的对象是一个实例,该符号字符'@'的类名称的字符串,散列$ C的无符号十六进制再presentation对象$ C。换句话说,该方法返回字符串等于的值:
的getClass()。的getName()+'@'+ Integer.toHexString(哈希code())
块引用>只需添加这
TriviaQuestion
:@覆盖公共字符串的ToString(){
返回玩家:+球员+;类别:+类别
+;问:+问题+,答:答案+
+;得分:+分;
}(或者使用
的String.format
的方法中做同样的事情。)I am taking a text file and filling an arraylist. To test the file I am printing it out before I move on. I am only able to see the memory address and not the actual info from the file. Is there something simple and probably obvious I'm missing?
public class TriviaQuestion { private String player; private String category; private String question; private String answer; private int score = 0; /** * */ public TriviaQuestion() { player = "unknown"; category = "unknown"; question = "unknown"; answer = "unknown"; score = 0; } public TriviaQuestion(String category, String question, String answer){ } public TriviaQuestion(String player, String category, String question, String answer, int score) { super(); this.player = player; this.category = category; this.question = question; this.answer = answer; this.score = score; } /** * @return the player */ public String getPlayer() { return player; } /** * @param player the player to set */ public void setPlayer(String player) { this.player = player; } /** * @return the category */ public String getCategory() { return category; } /** * @param category the category to set */ public void setCategory(String category) { this.category = category; } /** * @return the question */ public String getQuestion() { return question; } /** * @param question the question to set */ public void setQuestion(String question) { this.question = question; } /** * @return the answer */ public String getAnswer() { return answer; } /** * @param answer the answer to set */ public void setAnswer(String answer) { this.answer = answer; } /** * @return the score */ public int getScore() { return score; } /** * @param score the score to set */ public void setScore(int score) { this.score = score; } }
The tester
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class TriviaQuestionTester { /** * @param args */ public static void main(String[] args) throws IOException { File triviaFile = new File("trivia.txt"); Scanner triviaFile1 = new Scanner(triviaFile); String lastKnownCategory = ""; ArrayList<TriviaQuestion> myList = new ArrayList<TriviaQuestion>(); while (triviaFile1.hasNextLine()){ String currentLine = triviaFile1.nextLine(); if (!currentLine.isEmpty()) { TriviaQuestion currentQuestion = new TriviaQuestion(currentLine, currentLine, currentLine); if (currentLine.endsWith("?")) { currentQuestion.setCategory(lastKnownCategory); currentQuestion.setQuestion(currentLine); currentQuestion.setAnswer(triviaFile1.nextLine()); } else { currentQuestion.setCategory(currentLine); currentQuestion.setQuestion(triviaFile1.nextLine()); currentQuestion.setAnswer(triviaFile1.nextLine()); lastKnownCategory = currentLine; } myList.add(currentQuestion); } } triviaFile1.close(); System.out.println(myList); } }
解决方案Is there something simple and probably obvious I'm missing?
Yes - you haven't overridden
toString
inTriviaQuestion
, so you're getting the default implementation fromObject
:The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Just add this to
TriviaQuestion
:@Override public String toString() { return "Player: " + player + "; Category: " + category + "; Question: " + question + "; Answer: " + answer + "; Score: " + score; }
(Or use
String.format
within the method to do the same kind of thing.)这篇关于我从一个ArrayList获取内存地址,需要信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!