Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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();
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This:

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

should be

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

Then you create a Forfait object with the tokens. How you do this depends on the structure of this class (that we can’t see). You likely need to call its constructor and then put the object created into the tableauForfaits array, perhaps something like,

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

and you may need to parse numeric 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, not seeing the Forfait class or the text file.

You will need to create an int index before the while loop, increment it within the while loop, and use this to decide which row of the array to put your new object into.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading