[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
I made a program to grab usernames and total levels from the RuneScape high scores and output them. It worked fine until I added a for loop.
I input the number of ranks to the scanner and then the program terminates with no exceptions.
I am almost positive it is a logic problem with nested loops.
Help me out please, thanks!
Code:
/* Grab information from a web page */
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Networking {
static String line, username, totalLevel;
static int index, userUnCutLength, totalLevelUnCutLength;
public static void main(String[] args) throws Exception {
System.out.println("Connecting to http://services.runescape.com/m=hiscore/hiscores.ws ...");
URL url = new URL("http://services.runescape.com/m=hiscore/hiscores.ws");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
System.out.println("Successfully connected to http://services.runescape.com/m=hiscore/hiscores.ws !");
Scanner scanner = new Scanner(System.in);
System.out.println("Input # of ranks to grab");
int numberOfRanks = scanner.nextInt();
for (int f=0; f<numberOfRanks; f++){
while ((line=in.readLine()) != null) {
if ((index=line.indexOf(">"+f+"<")) < 0)
continue;
line=in.readLine().trim();
//grab username
line=line.substring(62);
userUnCutLength = line.length();
line=line.substring(0, userUnCutLength-9);
username = line;
//grab total level, it's the next line
line=in.readLine().trim();
line=line.substring(16);
totalLevelUnCutLength = line.length();
line=line.substring(0, totalLevelUnCutLength-5);
totalLevel = line;
System.out.println(username);
System.out.println(totalLevel);
}
}
}
}