Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Exclamation [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!

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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If the inner while loop is never executed then the very first read from th BufferedReader must be returning null. Time for you to debug why that is.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    If the inner while loop is never executed then the very first read from th BufferedReader must be returning null. Time for you to debug why that is.
    Nope, that is incorrect. If I change the "+f+" in the while loop to "1" it works.
    Last edited by Solarsonic; 03-22-2011 at 02:49 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    if ((index=line.indexOf(">"+f+"<")) < 0)
        continue;
    Another possibility is that this if statement is always true. Add a print statement to the if and see how many times it gets printed and for what values of 'f'.

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    if ((index=line.indexOf(">"+f+"<")) < 0)
        continue;
    Another possibility is that this if statement is always true. Add a print statement to the if and see how many times it gets printed and for what values of 'f'.
    That wouldn't be possible because it works with 1 without the "+f+" and if they input 1 it doesn't work.

    EDIT: Ohhhh my variable starts with 0 and it can't find that since the web page starts with 1. Problem fixed thanks :)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I just realised that there is a huge logic problem with your code (even if you fix f starting at 0).

    The inner loop executes until it exhausts the input. You then go back to the outer loop and increment f then back to the inner loop which can never be entered again as you have already exhausted the input.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    I just realised that there is a huge logic problem with your code (even if you fix f starting at 0).

    The inner loop executes until it exhausts the input. You then go back to the outer loop and increment f then back to the inner loop which can never be entered again as you have already exhausted the input.
    Ah, so how could I fix this? If I create a new BufferedReader for every iteration that would be slow as hell I'm guessing?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    How would that fix it. The problem is after the first time around the while loop there is nothing left to read.

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    How would that fix it. The problem is after the first time around the while loop there is nothing left to read.
    So I could add a break for the while loop?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    No, you would write logically correct code to read the data correctly. How about you fully explain what the input is and what the desired output should be.

  11. #11
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    No, you would write logically correct code to read the data correctly. How about you fully explain what the input is and what the desired output should be.
    My logic makes complete sense to me at least...

    I want to get an input for the number of ranks. For each of those ranks I want to do a while loop to find a line with that rank.

  12. #12
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Ok, new problem. I got it to work but it's one number short. Like if I type in "3" it'll print out the one that should be the 2nd. And if I type in 1 or 2 it types in the one that should be first.

    Java Code:
    for (int f=0; f<numberOfRanks; f++){
    		f++; //Ranks start at 1
    		
    		while ((line=in.readLine()) != null) {
    			if ((index=line.indexOf(">"+f+"<")) < 0) {
    				continue;
    			}
    			line=in.readLine().trim();
    			//grab username
    			line=line.substring(50);
    			userUnCutLength = line.length();
    			line=line.substring(0, userUnCutLength-9);
    			index=line.indexOf(">");
    			line=line.substring(0, index);
    			userUnCutLength = line.length();
    			line=line.substring(0, userUnCutLength-1);
    			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);
    			
    			break;
    			
    		}
    	}

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    for (int f=0; f<numberOfRanks; f++){
    		f++; //Ranks start at 1
    Blech!

    Why not just initialise f to 1?

    I still have no idea what you are trying to do unless you provide the information I requested earlier.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Actually that is your problem you are incrementing f twice.

    Begin with f at zero and enter loop.
    Increment f to 1
    Do while loop
    Return to for loop increment f to 2
    Enter for loop
    Increment f to 3 OH NO!

  15. #15
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    for (int f=0; f<numberOfRanks; f++){
    		f++; //Ranks start at 1
    Blech!

    Why not just initialise f to 1?

    I still have no idea what you are trying to do unless you provide the information I requested earlier.
    Because if they input 1 that means that numberOfRanks = 1 and then the for loop won't execute at all.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    So change the condition!

  17. #17
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    Actually that is your problem you are incrementing f twice.

    Begin with f at zero and enter loop.
    Increment f to 1
    Do while loop
    Return to for loop increment f to 2
    Enter for loop
    Increment f to 3 OH NO!
    I see what you're saying now where with the "f++" it stopped the loop either way because it equaled 1 again so therefore the loop must stop.

  18. #18
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I changed the condition to
    Java Code:
    for (int f=0; f<numberOfRanks+1; f++){
    and it still doesn't work? Could you please give me a clear fix? I don't really understand what you're saying...

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    :headdesk:

    Java Code:
    for (int f=1; f <= numberOfRanks; f++){

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

Page 1 of 2 12 LastLast

Similar Threads

  1. Nested loops
    By Aestuv in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 11:40 PM
  2. Nested Loops
    By joemama in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 09:17 PM
  3. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 06:03 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  5. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •