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”;

JavaScript

/ The Class Forfait :

JavaScript

Advertisement

Answer

This:

JavaScript

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

JavaScript

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