Skip to content
Advertisement

Java Extracting values from text files and put it into a array type class Forfait


Im new in java hope y’all doing great.
So i was trying to extract values from my text files and put into the array named tableauForfaits but i’m blocked can someone help me to see where’s my error because when i try to split it doesn’t work.

private static final String FIC_FORFAITS = “Forfaits.txt”;

// Déclaration des variables
private static Forfait tableauForfaits[] = new Forfait[6];


 * Read data from different packages (id | description | price)
 * in the "Forfaits.txt" file. The first line in this file is the
 * description of other lines and it should be ignored when
 * reading. The other lines of this file are each composed of the identifier,
 * of the description, and the price separated from each other by a vertical bar.
 * Each of these lines must be read and cut to create an object of type
 * Package, and this item should be added in the package table above
 * defined (see Forfaits.txt file for more details)
 *


public static void lireFichierForfaits() throws IOException,FileNotFoundException {
    try (BufferedReader reader = new BufferedReader (new FileReader(FIC_FORFAITS))) {
     
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            tableauForfaits = line.split("\|");
            for (String part : tableauForfaits) {
                System.out.println(part);
            }
            System.out.println();
        }
        reader.close();
    }
}

/ The Class Forfait :

private String identifiant;
private String description;
private float prix;


public Forfait (String identifiant, String description, float prix) {
    this.identifiant = identifiant;
    this.description = description;
    this.prix = prix;
}


public String getIdentifiant () {
    return identifiant;
}


public void setIdentifiant (String identifiant) {
    this.identifiant = identifiant;
}


public String getDescription () {
    return description;
}


public void setDescription (String description) {
    this.description = description;
}


public float getPrix () {
    return prix;
}


public void setPrix (float prix) {
    this.prix = prix;
}

Advertisement

Answer

This:

tableauForfaits = line.split("\|");

won’t work because String#split(...) returns an array of String, and you’re trying to force this into an array of a different type, a Forfait object.

Instead, that line should be

String[] tokens = line.split("\|");

This will hopefully split the row from the text file into individual small String tokens that can be used to help you create a single Forfait object with the tokens. How you create the object completely depends on the structure of this class (one that we are currently unable to see), but you likely need to call its constructor, using the String tokens obtained, and then put the Forfaitobject created into the tableauForfaits array, perhaps something like,

Forfait forfait = new Forfait(token[0], token[1].....);

Note that you may need to parse numeric token Strings to a numeric value before doing this, such as

Forfait forfait = new Forfait(Integer.parseInt(token[0]), token[1], .....);

Again, it’s impossible to say for sure, since we currently cannot see the Forfait class or the text file.

You will also need to create an int index variable before the while loop, increment it within the while loop, and use this as an index to the tableauForfaits array to help you decide which row of the array to put your newly create object into.

Advertisement