Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
    }
Posted
Updated 12-Apr-18 21:28pm
v2
Comments
CPallini 12-Apr-18 16:07pm    
You neither showed us the full code nor the input file content.

1 solution

Within your loop
Java
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
Java
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
Java
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:
Java
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).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900