简体   繁体   中英

Reading a file line and storing variables of different types

I am trying to figure out a way to read from the following text file. I am able to get the first integer in the text file which is numLines. After that point, I am able to get the first integer from the line, but am unable to successfully get each individual group of letters.

for(int i=0; i < numLines; i++){
   numVariables = Integer.parseInt(fin.next());

    for(int z=0; z < numVariables; z++){
        String line = fin.next();

        int numRules = Integer.parseInt(line.substring(0, 1));

        //Everything up until this point is good

        //read and store first capital letter of every line
        String variable = line.substring(2,3);

        //read and store remaining capital letters that correspond to every line separately
    }
}

Text File

3
2 A CC DD
3 A AA z v
2 F f a

I didn't see what is the problem. If you say you have problem of "parsing", you could try:

array =line.split(" ", 2)

then

array[0] is the leading number
array[1] is the rest letters

if you want each part, you could split (" ") without limit.

if you just want to get those Uppercase words, you can do it by regex:

line.split(" ",2); //array[0] is the leading number

apply "(?<= )[AZ]+(?= )" on array[1], you get all Upper case words.

is that what you want?

String[] words = line.split(("\\s+"); // Any whitespace
int numRules = Integer.parseInt(words[0]);
for (int j = 1; j < words.length; ++j) {
   String variable = words[j];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM