Results 1 to 20 of 21
- 03-22-2011, 02:31 AM #1
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
[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); } } } }
- 03-22-2011, 02:43 AM #2
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.
- 03-22-2011, 02:47 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 02:50 AM #4
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'.Java Code:if ((index=line.indexOf(">"+f+"<")) < 0) continue;
- 03-22-2011, 02:57 AM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:04 AM #6
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.
- 03-22-2011, 03:10 AM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:13 AM #8
How would that fix it. The problem is after the first time around the while loop there is nothing left to read.
- 03-22-2011, 03:22 AM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:26 AM #10
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.
- 03-22-2011, 03:31 AM #11
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:41 AM #12
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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; } }
- 03-22-2011, 03:43 AM #13
Blech!Java Code:for (int f=0; f<numberOfRanks; f++){ f++; //Ranks start at 1
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.
- 03-22-2011, 03:45 AM #14
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!
- 03-22-2011, 03:45 AM #15
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:49 AM #16
So change the condition!
- 03-22-2011, 03:51 AM #17
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-22-2011, 03:53 AM #18
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
I changed the condition to
and it still doesn't work? Could you please give me a clear fix? I don't really understand what you're saying...Java Code:for (int f=0; f<numberOfRanks+1; f++){
- 03-22-2011, 03:58 AM #19
:headdesk:
Java Code:for (int f=1; f <= numberOfRanks; f++){
- 03-22-2011, 03:58 AM #20
Similar Threads
-
Nested loops
By Aestuv in forum New To JavaReplies: 3Last Post: 02-10-2011, 11:40 PM -
Nested Loops
By joemama in forum New To JavaReplies: 1Last Post: 01-01-2011, 09:17 PM -
Nested Loops
By candygirl198827 in forum New To JavaReplies: 38Last Post: 12-01-2010, 06:03 AM -
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09-01-2009, 08:47 AM -
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 02:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks