Joshua Hester
Joshua Hester

Reputation: 127

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

Upvotes: 0

Views: 2093

Answers (1)

Databyte
Databyte

Reputation: 1481

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

Upvotes: 2

Related Questions