Skip to content
Advertisement

How to save information separately form .txt into multiple strings in Java

I have a mydata.txt, where I want to read in information separately.

The 1st row save as “zoo number”, “animal number”

the 2nd row till the 5th row save as “data for animals in matrix”

the 6th row save as dog “number” “appetite” “weight” “height”

JavaScript

The code I written as below, however, my code only be able to reload everything form mydata.txt into a string. But I want to save those information separately into individual string (for that matrix) and variables (E.g. dog’s number, dog’s sappetite…) . How can I do that?

JavaScript

Advertisement

Answer

Here you should use split method from String class.

JavaScript

So when you split with " " will give you array for String splitted by spaces. Then reading Index by Index will give you desired values.

Updated Answer: You can get value row by row as below :

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement