Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: Java Debugging

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Java Debugging

    I made a random number generator with GUI. This is what it does: ask for the range, parse range as int, ask how many numbers to generate, give a separate message (one after the other) for each number, ask if you want another set, and if yes, repeat (except for range). If no, it will record the data for1000 rolls at that range, and then (if requested) show it to them.


    Code:
    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.io.*;
    import java.util.Scanner;
    
    
    public class Main {
    	public static void main(String[] args) throws IOException {
    		String rangeS = JOptionPane.showInputDialog(null, "Enter Range", "6");
    		int range = Integer.parseInt(rangeS);
    		GEN(range);
    		
    		Random dc4 = new Random();
    		int freq[] = new int[range+1];
    		for(int roll = 0; roll < 1000; roll++){
    			++freq[1+dc4.nextInt(range)];
    		}
    		FileWriter w = new FileWriter(new File("frequencies.rck"));
    		w.write("Num\tFrequency\n");
    		for(int face = 1; face < freq.length; face++){
    			w.append(face + "\t" + freq[face] + "\n");
    		}
    		w.close();
    		int yn = JOptionPane.showConfirmDialog(null, "Done. Show roll stats?", "Done", JOptionPane.YES_NO_OPTION);
    		if (yn == JOptionPane.YES_OPTION){
    			Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
    			int n2 = range * 2;
    			int n = range;
    			int nl = 0;
    			int nlc = 0;
    			int nL = 0;
    			String r[] = new String[n];
    			while(nl <= n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    			}
    			String output = "";
    			int ct = 0;
    			while(nL <= n2){
    				output += r[ct];
    				output += ": ";
    				ct++;
    				output += r[ct];
    				output += "\n";
    				ct++;
    			}
    			JOptionPane.showMessageDialog(null, output, "Frequencies", JOptionPane.PLAIN_MESSAGE);
    		}
    	}
    	public static void GEN(int range) {
    		
    		int count = 0;
    		Random dc4 = new Random();
    		
    		while (count == 0){
    		String xt = JOptionPane.showInputDialog(null, "How many numbers?");
    		
    		int x = Integer.parseInt(xt);
    		for (int ct = 0; ct < x; ct++){
    			int d4raw = dc4.nextInt(range);
    			int d4 = d4raw + 1;
    			String d4o = Integer.toString(d4);
    			JOptionPane.showMessageDialog(null, d4o, "Random Number", JOptionPane.PLAIN_MESSAGE);
    		}
    		int ns = JOptionPane.showConfirmDialog(null, "All done. New set?", "Done!", JOptionPane.YES_NO_OPTION);
    		if (ns == JOptionPane.YES_OPTION){
    			count = 0;
    		}else{
    			count = 1;
    		}
    		
    	}
    	JOptionPane.showMessageDialog(null, " Recording Data;\n Hit \"Okay\" to continue", "Done!", JOptionPane.PLAIN_MESSAGE);
    	
    	}
    
    }
    It keeps throwing an error at line 34, and I can't find out why no matter how hard I scrutinize the script.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    is this line 34?
    Code:
    r[nlc] = rraw.next();
    no line number here so that helps nada... I see no problems w/ that section. What is the error that is thrown?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    is this line 34?
    Code:
    r[nlc] = rraw.next();
    no line number here so that helps nada... I see no problems w/ that section. What is the error that is thrown?
    Yes, that is line 34. Sorry about that. Also, the error is:


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Main.main(Main.java:34)
    it was previously line 33, and I used nl instead of two variables doing the same thing :P. That threw an error (the same one), so I tried making two separate variables instead.
    Last edited by Scriptr; 12-16-2011 at 09:10 PM.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I think...
    Code:
    String[] r = new String[n];
    not

    String r[] = new String[n];

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    But that has worked before, and that doesn't fix the problem.

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Quote Originally Posted by Scriptr View Post
    But that has worked before, and that doesn't fix the problem.
    checked a few more references and in Java you can declare an array like that- In C# that is a no-no! I would say for semantics you should do it that way but that is my opinion, take it or leave it.

    anyways onto the code- what IDE are you using? you should really learn debugging (I am not trying to sound like a prick- sorry if I do, the problem screamed at me the second I threw it into debug)... look at this loop...
    Code:
    while(nl <= n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    			}
    that code will run until nl == n and that will be that last time it runs.. all well and good right? but now let's step through that thought process... let's say our range.length turns out to be 3, so therefore n = 3... now let's step through the loop...
    when n=0 go through the loop, all is happy
    ~range[0]
    when n=1 go through the loop, all is happy
    ~range[1]
    when n=2 go through the loop, all is happy
    ~range[2]
    when n=3 go through the loop, all is not happy
    ~range[3]
    range is only [3] ie it is only range[0] range[1] range[2]
    little things like this pop up and smack you in the face when you debug (hence why I mentioned it)... I will admit it was stumping me until I threw it in there- mainly I was looking @ incorrect conversions and initializations and whatnot

    along those lines you should handle in your "ask for number" scans what If I enter "t" and hit ok? Error!

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    checked a few more references and in Java you can declare an array like that- In C# that is a no-no! I would say for semantics you should do it that way but that is my opinion, take it or leave it.

    anyways onto the code- what IDE are you using? you should really learn debugging (I am not trying to sound like a prick- sorry if I do, the problem screamed at me the second I threw it into debug)... look at this loop...
    Code:
    while(nl <= n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    			}
    that code will run until nl == n and that will be that last time it runs.. all well and good right? but now let's step through that thought process... let's say our range.length turns out to be 3, so therefore n = 3... now let's step through the loop...
    when n=0 go through the loop, all is happy
    ~range[0]
    when n=1 go through the loop, all is happy
    ~range[1]
    when n=2 go through the loop, all is happy
    ~range[2]
    when n=3 go through the loop, all is not happy
    ~range[3]
    range is only [3] ie it is only range[0] range[1] range[2]
    little things like this pop up and smack you in the face when you debug (hence why I mentioned it)... I will admit it was stumping me until I threw it in there- mainly I was looking @ incorrect conversions and initializations and whatnot

    along those lines you should handle in your "ask for number" scans what If I enter "t" and hit ok? Error!
    1: I use Eclipse
    2: I learned to do an array like that from tutorials by thenewboston on youtube, and (going from ASP to PHP oftenish) I've just given up on trying to make my code as universal as possible. I don't even format my function layers the same way between javascript, PHP, C, and Java.
    3: How is the range only 3? (Obviously, that won't show up in debugging) Could you explain to me how I went wrong?
    4: I know it throws an error when you enter text. I just want to get it working first. this is still (pre-)alpha. That is a beta-level debug, and I want to finish the pre-alpha- and alpha- level debugs first.

  • #8
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    "3" because when you threw the box asking me I said 3 instead of leaving it at six... could do the same thing for default of 6, but then I would have been typing more... leaving @ 6 you would drop into
    n=6 go through the loop (note nlc is also 6) and now look at range[6]
    range only goes to range[5]... arrays start at 0
    Code:
    string[] range = new string[6];
    range[0] = "position '1' on the string @ 0";
    range[1] = "position '2' on the string @ 1";
    range[2] = "position '3' on the string @ 2";
    range[3] = "position '4' on the string @ 3";
    range[4] = "position '5' on the string @ 4";
    range[5] = "position '6' on the string @ 5";
    range[6] in this instance (which code is attempting to access) is not in existance

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #9
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    I removed part of it (the nlc variable; I figured out that wasn't throwing the error from your first couple of posts and just trial and error), so it is now this:
    Code:
    if (yn == JOptionPane.YES_OPTION){
    
    			Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
    
    			int n2 = range * 2;
    			int n = range;
    			int nl = 0;
    			int nL = 0;
    
    			String r[] = new String[n-1];
    			while(nl <= n){
    				r[nl] = rraw.next();
    				nl++;
    			}
    			String output = "";
    			int ct = 0;
    			while(nL <= n2){
    				output += r[ct];
    				output += ": ";
    				ct++;
    				output += r[ct];
    				output += "\n";
    				ct++;
    			}
    			JOptionPane.showMessageDialog(null, output, "Frequencies", JOptionPane.PLAIN_MESSAGE);
    		}
    (note, I also subtracted one from the array. In fact, I subtracted 1, then 2, then 3, until the end result was 0, and it still threw out of bounds exception)

  • #10
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    I am an idiot: I needed to add one, not subtract. However, now it throws an error at line 49 (I separated the lines logically, so the numbers are different. You want output += r[ct];)
    and if you fix that with this:
    Code:
    while(ct < n2){
    				output += r[ct];
    				output += ": ";
    				ct++;
    				if (ct < n2){
    				output += r[ct];
    line 50				output += "\n";
    					ct++;
    				}else{
    					ct = n2+1;
    				}
    			}
    it throws an error (the same one) at line 50. When I made it work, it left out the last statistic (but not the corresponding number)
    Last edited by Scriptr; 12-17-2011 at 03:31 AM.

  • #11
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    i dont know what your error is- need to post it, but no, do not add one anywhere... go back to your original code and change these parts...
    Code:
    int n = range;
    int nlc = 0;
    String r[] = new String[n];
    while(nlc < n){
    r[nlc] = rraw.next();
    nlc++;
    }
    or for a cleaner look and feel
    Code:
    String[] r = new string[range];
    for(int i=0; i<= r.length-1; i++){
    r[i] = rraw.next();
    }
    
    or
    
    String[] r = new string[range];
    for(int i=0; i< r.length; i++){
    r[i] = rraw.next();
    }
    see how tighter that code is?
    what is error @ line 49? I closed eclipse and with it ur project (ie idk where/what line 49 is and I can't replicate error)

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    Scriptr (12-17-2011)

  • #12
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    that part is clean now. I put String r[] = new String[n+1]; instead of String r[] = new String[n]; and it didn't throw any errors inside that while loop. output += r[ct]; is the line the error is on, and this:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
    at Main.main(Main.java:50)

    is the error. The problem might be that I added 1. I'll try what you did and see if that fixes it.


    Nope. Didn't fix it.
    Last edited by Scriptr; 12-17-2011 at 05:26 AM.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Eclipse is great for debugging Java. Simply apply a break point to a logical preceding point before the error, and evaluate the variables as you have received them running them through debug mode. It will issue the break point when you reach it.
    You are approaching the error backwards. Let's take your original code:
    Code:
    Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
    			int n2 = range * 2;
    			int n = range;
    			int nl = 0;
    			int nlc = 0;
    			int nL = 0;
    			String r[] = new String[n];
    			while(nl <= n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    			}
    			String output = "";
    			int ct = 0;
    			while(nL <= n2){
    				output += r[ct];
    				output += ": ";
    				ct++;
    				output += r[ct];
    				output += "\n";
    				ct++;
    			}
    As mentioned, your iteration is too high. One of the first things to know about arrays in the language you are dealing with is if the arrays are 0 based, or 1 based (Java is 0 based, VB is 1 based for example). This means when you have a size of 3, it is 0, 1, 2, not 1, 2, 3. Alykins pointed this out. The easiest fix for your first error is right here:
    Code:
    			String r[] = new String[n];
    			while(nl <= n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    String[] is size 3, so you have 0, 1, 2. But you are iterating nl to <= n, which is 3. So this starts at 0, and iterates up to and including 3. This should simply be:
    Code:
    			String r[] = new String[n];
    			while(nl < n){
    				r[nlc] = rraw.next();
    				nl++;
    				nlc++;
    < so that you stop when you reach the three.

    For your next error, its size is way to big:
    Code:
    			int n2 = range * 2; // Range is 6
    ...
    			while(nL <= n2){
    				output += r[ct];
    You'll never be able to iterate r[ct] up to that of n2. You have 8 items in total, not 12, but you'll still lose out on 2 of them since the data is missing the last two entries. If you use two loops like this, base the second on the r.length instead, so using a for loop makes more sense.

    This entire block is much easier written when depending on the Scanner if you just want a string:
    Code:
    			Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
    			String output = new String("");
    			while (rraw.hasNext())
    			{
    				output += rraw.nextLine() + "\n";
    			}
    			System.out.println(output);
    Note that you cannot use the JOptionDialog to display this as it doesn't parse tabs. For this, you need to use a component capable of rendering them, such as the JTextArea. This can be added to the JOptionDialog as well:
    Code:
    			Scanner rraw = new Scanner(new FileInputStream("frequencies.rck"));
    			String output = new String("");
    			while (rraw.hasNext())
    			{
    				output += rraw.nextLine() + "\n";
    			}
    			JTextArea jta = new JTextArea(output);
    			jta.setEditable(false);
    			jta.setOpaque(false);
    			JOptionPane.showMessageDialog(null, jta, "Frequencies", JOptionPane.PLAIN_MESSAGE);

  • Users who have thanked Fou-Lu for this post:

    Scriptr (12-17-2011)

  • #14
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you, thank you, thank you, thank you! I knew about the problem with tabs. I had set it up so that it would insert a ": " in between. That block of code was the root of all my problems. Showing me JTextArea helped a LOT! Now I am done with the alpha version and I can start the Beta 1.1 downloads. Those are simple if...else, a few JOptionPane, et cetera. I can do that myself.


  •  

    Posting Permissions

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