为什么我的代码只读取最后一个元素? [英] Why does my code reads only the last element?

查看:81
本文介绍了为什么我的代码只读取最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我试图读取存储在文本文件中的几种技能类型,这些技能类型是一个接一个地分开的。在17个技能中,我的代码只显示最后一行为什么?



我尝试过:



 //主程序
BufferedReader in = Skills.getSkill(/ Users / natasha / IdeaProjects / CW02_w1698409 / Skills.txt);
技能技能= Skills.readSkill(in);
while(技能!= null){
skills = Skills.readSkill(in);

}
Skills.Skill();
Skills.print();



 //技能等级
public static技能readSkill(BufferedReader in){
字符串名称;
字符串可选;
String short_desc;
int stat_affinity;
int rank;
技能next_Skill = null;
String line =;
String []数据;

try {
line = in.readLine();
} catch(IOException e){
System.out.println(I / O异常); //取消注入语言
System.exit(0);
}
if(line!= null){
data = line.split(\t);
name = data [0];
可选=数据[1];
short_desc = data [2];
stat_affinity = Integer.parseInt(data [3]);
rank = Integer.parseInt(data [4]);
返回新技能(名称,可选,short_desc,stat_affinity,rank,next_Skill);
}
返回null;} public static void print(){

System.out.println(名称:+名称);
System.out.println(可选:+可选);
System.out.println(简短描述:+ short_desc);
System.out.println(统计亲和力:+ stat_affinity);
System.out.println(等级:+等级);
System.out.println(下一个技能:+ next_Skill);
} public static BufferedReader getSkill(String name){
BufferedReader in = null;
try {
文件file = new File(name);
in = new BufferedReader(new FileReader(file));

} catch(FileNotFoundException e){
System.out.println(文件不存在);
System.exit(0);
}
返回;在$ $ $ $ $ $ $ class =code-keyword> while (技能!= null){
skills = Skills.readSkill(in);
}

你正在阅读文件结尾而不打印任何东西。



你终于得到了最后一行的数据打印在

 Skills.print(); 

可能是通过将数据字段成员声明为 static 也是在将数据传递给构造函数时设置的

  return   new 技能(名称,可选,short_desc,stat_affinity,rank,next_Skill); 

但这只是猜测因为你没有向我们展示声明这些成员和构造函数。



要打印每行的数据,你必须在循环中这样做:

 skills = Skills.readSkill(in); 
while (技能!= null){
skills.print();
技能= Skills.readSkill(in);
}

还要注意我已经使用返回的对象技巧来打印而不是 static function Skills.print()



我建议重新阅读你的Java教程关于使用 static 。仅在必要时使用 static 数据成员和函数。类的一个最重要的特性是每个实例都有自己的数据成员副本。但是当使用 static 成员时这不适用。



我建议重新设计完整的课程,不要使用任何 static 成员(数据和函数)。


In my program I'm trying to read several skill types stored in text file which are separated one after the other.out of the 17 skills my code display only the last line why is that?

What I have tried:

 // main program
BufferedReader in = Skills.getSkill("/Users/natasha/IdeaProjects/CW02_w1698409/Skills.txt");
        Skills skills= Skills.readSkill(in);
        while(skills != null) {
            skills = Skills.readSkill(in);

        }
        Skills.Skill();
        Skills.print();


//skill class
public static Skills readSkill(BufferedReader in) {
        String name;
        String optional;
        String short_desc;
        int stat_affinity;
        int rank;
        Skills next_Skill = null;
        String line = "";
        String[] data;

        try {
            line = in.readLine();
        } catch (IOException e) {
            System.out.println("I/O Exception");//un-inject language
            System.exit(0);
        }
        if (line != null ){
            data = line.split("\t");
            name = data[0];
            optional = data[1];
            short_desc = data[2];
            stat_affinity = Integer.parseInt(data[3]);
            rank = Integer.parseInt(data[4]);
            return new Skills(name, optional, short_desc, stat_affinity, rank, next_Skill);
        }
        return null;}public static void print()  {

        System.out.println("The name: "+name);
        System.out.println("The optional: "+optional);
        System.out.println("The short description: "+short_desc);
        System.out.println("The stat affinity: "+stat_affinity);
        System.out.println("The ranks: "+rank);
        System.out.println("The next skill: "+next_Skill);
    }public static BufferedReader getSkill(String name) {
        BufferedReader in = null;
        try {
            File file = new File(name);
            in = new BufferedReader(new FileReader(file));

        } catch (FileNotFoundException e) {
            System.out.println("file does not exist");
            System.exit(0);
        }
        return in;
    }

解决方案

Within your loop

while(skills != null) {
    skills = Skills.readSkill(in);
}

You are reading until the end of file without printing anything.

That you finally got the data of the last line printed at

Skills.print();

is probably sourced by declaring the data field members as static too which are set when passing the data to the constructor at

return new Skills(name, optional, short_desc, stat_affinity, rank, next_Skill);

But that is just a guess because you have not shown us the declaration of these members and the constructor.

To print the data of each line you have to do that in the loop:

skills = Skills.readSkill(in);
while (skills != null) {
    skills.print();
    skills = Skills.readSkill(in);
}

Note also that I have used the returned object skills there to print and not the static function Skills.print().

I suggest to re-read your Java tutorials about the use of static. Use static data members and functions only where necessary. One of the most important features of classes is that each instance has it's own copy of data members. But this does not apply when using static members.

I suggest to redesign the complete class to not use any static members (data and functions).


这篇关于为什么我的代码只读取最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆