当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Make Multiple Strings with For Loop

问题描述:

So I have a text file reader that reads in each string as they come in.

String R1Name = fileScanner.nextLine();

String R1URL = fileScanner.nextLine();

String R2 = fileScanner.nextLine();

String R2Name = fileScanner.nextLine();

String R2URL = fileScanner.nextLine();

String R3 = fileScanner.nextLine();

String R3Name = fileScanner.nextLine();

String R3URL = fileScanner.nextLine();

Etc.

How can I have it so it creates a simple for loop with number 1-7 that does the same thing with less code?

for(int i=0; i<8; i++){

String R(i) = fileScanner.nextLine();

String R(i)URL=fileScanner.nextLine();

}

Trying to get something like this but with no luck

网友答案:

You can use an array:

String[] R = new String[8];
String[] RUrl = new String[8];

for(int i = 0; i < 8; i++)
{
    R[i] = fileScanner.nextLine();
    RUrl[i] = fileScanner.nextLine();
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: