0

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

1 Answer 1

2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.