Java Debug Issue

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Java Debug Issue

Post by T3rminus on Wed Jun 22, 2011 4:10 pm
([msg=58855]see Java Debug Issue[/msg])

Here is the code:

Code: Select all
import java.util.Random;
class imp{
   public static void main (String [] args){
      Random rnum = new Random ();
      int red[]=new int[7];
      
      for(int roll=0; roll<1000; roll++){
         ++red[1+rnum.nextInt(6)];
      }
      
      System.out.println("Aray Num:\tTimes Shown:");
      
      for(int face=1;face<=red.length; face++){
         System.out.println(face+"\t"+red[face]);
         }
      }
   }


I am not getting any compilation errors. When I run it doies what it is supposed to, but there is an error at the end that says:

Code: Select all
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
   at imp.main(imp.java:14)


I cant see the error. The program does exactly what I want it to...

Thx in advance :)

-Phill
Philosopher - T3rminus
Image
http://lunchboxproductions.webs.com
Basic: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Realistic: (1) (2) (3)
Application: (1) (2)
Javascript: (1) (2)
User avatar
T3rminus
New User
New User
 
Posts: 13
Joined: Fri Aug 15, 2008 4:16 pm
Location: ∙∙∙∙∙∙...∙∙∙∙∙∙
Blog: View Blog (0)


Re: Java Debug Issue

Post by JoeyPardella on Wed Jun 22, 2011 4:26 pm
([msg=58856]see Re: Java Debug Issue[/msg])

if you write to red[7] it'll be out of bounds because the 0 counts as first element.
so you'll have to nextInt(5) or red = new int[8]
JoeyPardella
Experienced User
Experienced User
 
Posts: 81
Joined: Tue Jan 04, 2011 8:43 am
Blog: View Blog (0)


Re: Java Debug Issue

Post by T3rminus on Wed Jun 22, 2011 6:35 pm
([msg=58871]see Re: Java Debug Issue[/msg])

Well if you watch this video the code is identical to his. Shouldn't this work. Or is my code wrong and I just dont know it???

http://www.youtube.com/watch?v=pHxtKDENDdE&NR=1&feature=fvwp

-Phill
Philosopher - T3rminus
Image
http://lunchboxproductions.webs.com
Basic: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Realistic: (1) (2) (3)
Application: (1) (2)
Javascript: (1) (2)
User avatar
T3rminus
New User
New User
 
Posts: 13
Joined: Fri Aug 15, 2008 4:16 pm
Location: ∙∙∙∙∙∙...∙∙∙∙∙∙
Blog: View Blog (0)


Re: Java Debug Issue

Post by JoeyPardella on Thu Jun 23, 2011 2:15 am
([msg=58882]see Re: Java Debug Issue[/msg])

ok I looked over the code again.

It's not the random stuff. but it IS worthy to know that random.nextInt(int i) gets values from 0 (including) to i (excluding). that's why new int[7] is fine.

what't NOT fine though is:

Code: Select all
for(int face=1;face<=red.length; face++){
          System.out.println(face+"\t"+red[face]);
       }


as a rule of thumb you can remember that it is never a good idea to write i<=something.length
it always has to be i<=something.length-1 or it can also be i<something.length.
but you can't read the array position on length. that'll throw an out of bounds exception.

here's the complete correct code:

Code: Select all
package test;

import java.util.Random;

public class Test {

   /**
    * @param args
    */
   public static void main(String[] args) {
      Random rnum = new Random ();
       int red[]=new int[7];
        
       for(int roll=0; roll<1000; roll++){
          ++red[1+rnum.nextInt(6)];
       }
        
       System.out.println("Aray Num:\tTimes Shown:");
        
       for(int face=1;face<red.length; face++){
          System.out.println(face+"\t"+red[face]);
       }

   }

}




edit: it's even right in the video dude.
also let me add that if you loop from i=1 to i<1000 It'll just loop 999 times and not 1000. that's a mistake he made in the video and I think you should move to some better sources than this.
JoeyPardella
Experienced User
Experienced User
 
Posts: 81
Joined: Tue Jan 04, 2011 8:43 am
Blog: View Blog (0)


Re: Java Debug Issue

Post by jpmut on Thu Jun 23, 2011 8:02 pm
([msg=58923]see Re: Java Debug Issue[/msg])

Now you should learn to read Java Error Messages. Saves a lot of debugging time.

It said:
Code: Select all
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
   at imp.main(imp.java:14)


Meaning, some array (say -temp-) item is being called by temp[7] and yet temp's size is less than or equal to 7, and this call happens in the function main() on the 14-th line of imp.java

The only call/reference of an array on that line is red[face], and the error tells us face=7 at some point which is not supposed to happen since we know red has size 7. How do we constrain the size of face then, fixing the condition in the for-loop.

;)
The philosopher and mathematician Bertrand Russell was asked, "If a false premise can imply any conclusion, use 1 = 2 to prove you're the Pope!" Russell replied, "The Pope and I are two, so we are one."
User avatar
jpmut
New User
New User
 
Posts: 22
Joined: Fri Jul 03, 2009 1:13 pm
Blog: View Blog (0)


Re: Java Debug Issue

Post by T3rminus on Fri Jun 24, 2011 7:15 am
([msg=58934]see Re: Java Debug Issue[/msg])

Thx a lot for all of the help guys. I really need some encoragement to keep going with programming. I learn it really quickly but I cant stick with it for very long. Anyway thx. I will be going at it again really hard when I wake up. Keep up the good help.

-Phill
Philosopher - T3rminus
Image
http://lunchboxproductions.webs.com
Basic: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Realistic: (1) (2) (3)
Application: (1) (2)
Javascript: (1) (2)
User avatar
T3rminus
New User
New User
 
Posts: 13
Joined: Fri Aug 15, 2008 4:16 pm
Location: ∙∙∙∙∙∙...∙∙∙∙∙∙
Blog: View Blog (0)


Re: Java Debug Issue

Post by MarkIntelhunter99 on Sat Apr 07, 2012 4:27 pm
([msg=65531]see Re: Java Debug Issue[/msg])

Your program accessed more than the original array's length.
Code: Select all
import java.util.Random;
class Debug{
   public static void main (String [] args){
      Random rnum = new Random ();
      int red[]=new int[7];

      for(int roll=0; roll<1000; roll++){
         ++red[1+rnum.nextInt(6)];
      }

      System.out.println("Aray Num:\tTimes Shown:");

      for(int face=1;face<red.length; face++){
         System.out.println(face+"\t"+red[face]);
         }
      }
   }



and another way to Generate random number is

Code: Select all
class TestRandom{
   public static void main (String args[]) {
      int N = 1000, count = 0, number;
      double X;

      do{
         count++;
         X = Math.random();
         number = (int) Math.floor ( X * 1000 ) + 1;
         System.out.println(number);
      } while (count < N && 1 <= number && number <= 5);


   }
}


if you are trying to do so.
MarkIntelhunter99
New User
New User
 
Posts: 7
Joined: Thu Apr 05, 2012 12:48 pm
Blog: View Blog (0)


Re: Java Debug Issue

Post by fashizzlepop on Sat Apr 07, 2012 4:38 pm
([msg=65534]see Re: Java Debug Issue[/msg])

This is a quasi-necro. The problem looked solved and it's been more than half a year since the last post. No warning, just watch out.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Moderator
Moderator
 
Posts: 2145
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests