Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2003

Tip: Looking for answers? Try searching our database.

runtime error

Thread view: 
os2 - 31 Oct 2003 21:18 GMT
hi

when i compile this code i got any error...
but when i run it i get this error:

E:\divers\ets\LOG\log320\aut03\lab1>java Sort
Exception in thread "main" java.lang.ClassCastException
        at Sort.radixSort(Sort.java:30)
        at Sort.statistique(Sort.java:101)
        at Sort.main(Sort.java:139)

import java.util.*;
import java.io.*;
import java.lang.*;

//http://www.theory.informatik.uni-kassel.de/~dieter/algo/programmeJava/kapitel5/S
ort.java


public class Sort {

  Long[]a;
  long baro=0;
  long[][] tabStat= new long[50][6];
  int maxTest=50;

  //algo du tri radixSort
  public void radixSort( Long a[], int k, int log) {
    LinkedList[] list = new LinkedList[ log ];
    for( int i = 0; i < log; i++ ){
      list[ i ] = new LinkedList( );
      baro++;
    }

    LinkedList tmpList = new LinkedList( );
    for( int i = 0; i < a.length; i++ ){
      tmpList.addLast( a[ i ] );
      baro++;
    }

    for( int i = 0, mult = 1; i < k; i++ ) {
      while( !tmpList.isEmpty( ) ) {  //line 30
    Long p = (Long)tmpList.removeFirst( );
    list[ ( p.intValue( ) / mult ) % log ].addLast( p );
    baro++;
      }

      for( int j = 0; j < log; j++ ) {
    tmpList.addAll( list[ j ] );
    list[ j ].clear( );
    baro++;
      }
      mult *= log;
    }
    tmpList.toArray( a );
  }

  //write data
  public void ecrireFichier(Long a[], String NomFichier){
    try{
      PrintWriter out  = new PrintWriter(new FileWriter(NomFichier));
      for (int i = 0; i < a.length; i++)
        out.println(a[i]);
      out.close();
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  //write data
  public void ecrireBaro(long[][] tabStat, String NomFichier){
    try{
      PrintWriter out  = new PrintWriter(new FileWriter(NomFichier));
      for(int i=0;i<maxTest;i++){
        out.println(tabStat[i][0] );
        out.println(tabStat[i][1] );
        out.println(tabStat[i][2] );
        out.println(tabStat[i][3] );
        out.println(tabStat[i][4] );
        out.println(tabStat[i][5] );
        out.println("=============" );
        out.close();
      }
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  //create value for the array
  public void genererTableau(Long a[]){
    long rang=4294967296l;  //rang
    Random r1 = new Random();
    for (int i=0;i<a.length;i++){
      a[i] = new Long ( (long) (r1.nextDouble()*rang));
    }
  }

  public void statistique(){
    int n=100; //element number array
    a = new Long[n];
    Long []original;
    original = new Long[n];

    for(int i=0;i<maxTest;i++){

      genererTableau(original);

      a=(Long[])original.clone();

      //base 2 - passe k
      radixSort( a, 32,2 );  //line 101
      tabStat[i][0]=baro;

      //base 8 - passe k/3
      a=(Long[])original.clone( );
      baro=0;
      radixSort( a, 32/3,8 );
      tabStat[i][1]=baro;

      //base 16 - passe k/4
      a=(Long[])original.clone( );
      baro=0;
      radixSort( a, 32/4,16 );
      tabStat[i][2]=baro;

      //base 64 - passe k/6
      a=(Long[])original.clone( );
      baro=0;
      radixSort( a, 32/6,64 );
      tabStat[i][3]=baro;

      //base racine(n) - passe k/log2(racine(n))
      a=(Long[])original.clone( );
      baro=0;
      radixSort( a, (int)(Math.sqrt(n)),
(int)(32/(Math.log(Math.sqrt(n))/Math.log(2.0))) );
      tabStat[i][5]=baro;

      //base racine 3ieme(n) - passe k/log2(racine 3ieme(n))
      a=(Long[])original.clone( );
      baro=0;
      radixSort( a, (int)(Math.pow(3,1/n)),
(int)(32/(Math.log(Math.pow(3,1/n))/Math.log(2.0))) );
      tabStat[i][6]=baro;
    }
    ecrireBaro(tabStat,"stat.txt");
  }

  public static void main( String[] args ) {
    Sort tmp = new Sort();
    tmp.statistique();  //line 139
  }

}

any idea to resolve that?
Signature

Borland rulez http://pages.infinit.net/borland

--
Borland rulez http://pages.infinit.net/borland

Kevin Wright - 01 Nov 2003 09:49 GMT
Hmmm,

When I  run your code, I get an ArrayIndexOutOfBoundsException due to an
invalid
index on your 'list' array in your radixSort method being generated.

The following debugging code:
Long p = (Long)tmpList.removeFirst( );
System.out.println("size of list is " + list.length + " log is set to " +
log);
System.out.println("mult is set to " + mult + " p is set to " +
p.intValue() );
System.out.println("evaluation of array index value is " + ((p.intValue() /
mult) % log) );

list[ ( p.intValue( ) / mult ) % log ].addLast( p );

generated:

size of list is 2 log is set to 2
mult is set to 1 p is set to -1778271707
evaluation of array index value is -1
java.lang.ArrayIndexOutOfBoundsException: -1
at Sort.radixSort(Sort.java:49)
at Sort.statistique(Sort.java:132)
at Sort.main(Sort.java:180)
Exception in thread "main"

Kevin Wright.

> hi
>
[quoted text clipped - 10 lines]
> import java.io.*;
> import java.lang.*;

//http://www.theory.informatik.uni-kassel.de/~dieter/algo/programmeJava/kapi
tel5/Sort.java

> public class Sort {
>
[quoted text clipped - 135 lines]
>
> any idea to resolve that?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.