Results 1 to 4 of 4
  1. #1
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Arrow What is wrong with my code?

    I created a program to grab usernames by rank from a web page. The number of ranks to get is inputted via a System.in scanner and every so often (as defined in my code), it will change pages to get more ranks.

    I looked over my code many times and have been trying to fix this for hours.

    My output: (undesired)
    Java Code:
    Welcome to Solar's RS Username Grabber.
    
    Connecting to http://services.runescape.com/m=hiscore/hiscores.ws ...
    Successfully connected to http://services.runescape.com/m=hiscore/hiscores.ws !
    
    Please input the number of ranks to be grabbed.
    10
    Jdelacroix
    
    Program Terminated (Eclipse)
    Jdelacroix is the first rank on the page. It should be showing the first 10 ranks as I asked. I'm almost positive that the problem lies in one of the loops.

    My code (where problems arise):
    Java Code:
    lines =  new String[numberOfRanks];
    	switchPages = new int[numberOfRanks/23];
    	int pageTracker = 23; //starts with 23 because connection to the first page is already established earlier on in the code
        for (int i=0; i< switchPages.length; i++) { //fill up an array with rank values which determine when to switch pages
    		switchPages[i] = pageTracker;
    		pageTracker+=22; //every page has 22 ranks
    	}
    	
    	for (int f=1; f<=numberOfRanks; f++){
    		for (int i : switchPages) {
    			if (i == f) {
                                    //Change pages - the objects have already been defined earlier for the first page
    				url = new URL("http://services.runescape.com/m=hiscore/overall.ws?rank="+f+"&table=0&scroll=true&category_type=0");
    				System.out.println("Connecting to "+url+" for ranks "+f+"-"+(f+21));
    				in.close();
    				conn = url.openConnection();			
    				in = new BufferedReader
    				(new InputStreamReader(conn.getInputStream()));
    				break;
    		}
    		}
    		while ((line=in.readLine()) !=null) {
    			if ((index=line.indexOf(">"+f+"<")) < 0)
    				continue;
    			line=in.readLine().trim();
    			line=line.substring(50);
    			index=line.indexOf(">");
    			userUnCutLength = line.length();
    			line=line.substring(index+1, userUnCutLength-9);
    			username = line;
    			System.out.println(username);
    			
    		}	
    }
    I would appreciate it very much if you could help me out, thanks a lot in advance!
    ~Solarsonic
    Last edited by Solarsonic; 03-22-2011 at 10:27 PM.

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

    Default

    As I explained in your other thread the inner while loop consumes all data and only stops when there is nothing more to read. So outer for loop f = 1, then inner while loop reads a line and it finds 1 so it prints output. Then go around while loop again and read data which has 2 on it and since f is still 1 it enters the if and continues. Reads line 3, still does not match 1, continue. etc until end of input. Then it goes back to outer for loop and changes f to 2. Back to while loop which is not entered since there is no more input. Back to outer for loop and change f to 3. back to while loop which is not entered etc etc.

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

    Default

    Quote Originally Posted by Junky View Post
    As I explained in your other thread the inner while loop consumes all data and only stops when there is nothing more to read. So outer for loop f = 1, then inner while loop reads a line and it finds 1 so it prints output. Then go around while loop again and read data which has 2 on it and since f is still 1 it enters the if and continues. Reads line 3, still does not match 1, continue. etc until end of input. Then it goes back to outer for loop and changes f to 2. Back to while loop which is not entered since there is no more input. Back to outer for loop and change f to 3. back to while loop which is not entered etc etc.
    Ah, a simple break statement fixed the problem. Thanks a lot once again man :) My program is just about finished thanks to you. Will definitely remember to check these things in the future.

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

    Default

    No, using a break statement is not correct. The inner while loop is pointless. Just use your outer for loop to control how much data to read.

Similar Threads

  1. what is wrong with my code
    By amrmb09 in forum New To Java
    Replies: 7
    Last Post: 02-11-2011, 05:10 PM
  2. what is wrong with my code???
    By EBBOOO in forum New To Java
    Replies: 30
    Last Post: 07-27-2010, 09:59 PM
  3. Please tell me what i am doing wrong in this code
    By Basit781 in forum CLDC and MIDP
    Replies: 11
    Last Post: 05-20-2010, 05:08 PM
  4. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 09:30 AM
  5. what's the wrong in this Code
    By the swan in forum AWT / Swing
    Replies: 1
    Last Post: 04-04-2009, 04:27 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
  •