Link to home
Start Free TrialLog in
Avatar of mmccy
mmccy

asked on

Can not run this program !!

I got help from one expert(thank you) and have the following program, this can be compiled but once I run it and it has errors below, Can anyone help me to solve the problem ? I have put the Cleaner.class, testFile in the folder "C:\Sun\AppServer\jdk\bin"(classpath) !!
Thank you so much !!!

Exception in thread "main" java.lang.NumberFormatException: For input string: ""

        at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
        at java.lang.Integer.parseInt(Integer.java:489)
        at java.lang.Integer.parseInt(Integer.java:518)
        at Cleaner.readFile(Cleaner.java:31)
        at Cleaner.main(Cleaner.java:141)
Press any key to continue...

------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Cleaner
{
    //~ Static fields/initializers ---------------------------------------------------------------------------------------------------------

    static final int NUM_COLS = 17;

    //~ Methods ----------------------------------------------------------------------------------------------------------------------------

    public ArrayList readFile( String fileName ) throws java.io.FileNotFoundException
    {
        ArrayList ret = new ArrayList(  );
        try
        {
            BufferedReader fos = new BufferedReader( new FileReader( fileName ) );

            String         line = fos.readLine(  );
            while ( line != null )
            {
                String[] array   = line.split( "," );
                int[]    numbers = new int[ NUM_COLS ];
                for ( int idx = 0; idx < NUM_COLS; idx++ )
                {
                    numbers[ idx ] = Integer.parseInt( array[ idx ] );
                }

                line = fos.readLine(  );
                ret.add( numbers );
            }

            fos.close(  );
        }
        catch ( IOException ex )
        {
            System.out.println( ex );
        }

        return ret;
    }

    static boolean delete( int[] listOfIntegers )
    {
        boolean delete = false;
        Arrays.sort( listOfIntegers );

        int median      = listOfIntegers[ listOfIntegers.length / 2 ];
        int targetValue = median * 3;

        for ( int i = 0; ( i < listOfIntegers.length ) && ( !delete ); i++ )
        {
            int testValue = listOfIntegers[ i ];
            delete = testValue > targetValue;
            System.out.println( "test=" + testValue + ", target=" + targetValue );
        }

        return delete;
    }

    private static int min( int[] list )
    {
        int min = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] < min )
            {
                min = list[ idx ];
            }
        }

        return min;
    }

    private static int max( int[] list )
    {
        int max = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] > max )
            {
                max = list[ idx ];
            }
        }

        return max;
    }

    static int[] normalize( int[] list )
    {
        int[] normalized  = new int[ list.length ];
        int   min         = min( normalized );
        int   maxMinusMin = max( normalized ) - min;

        for ( int i = 0; ( i < list.length ); i++ )
        {
            int s = list[ i ];

            // calculate
            int calculated = ( s - min ) / maxMinusMin;
            normalized[ i ] = calculated;
        }

        return normalized;
    }

    public ArrayList cleaning( ArrayList list )
    {
        for ( int idx = 0; idx < list.size(  ); idx++ )
        {
            int[] line = (int[])list.get( idx );
            if ( delete( line ) )
            {
                list.remove( idx );
            }
        }

        return list;
    }

    public static void main( String[] args )
    {
        /*if ( args.length == 0 )
        {
            throw new IllegalArgumentException( "Usage: Cleaner filename " );
        }*/

        Cleaner   cleaner = new Cleaner();

        ArrayList lines;
        try
        {
            //lines = cleaner.readFile( args[ 0 ] );
            lines = cleaner.readFile( "testFile" );

            ArrayList cleaned = cleaner.cleaning( lines );

            for ( int idx = 0; idx < cleaned.size(  ); idx++ )
            {
                int[] line       = (int[])cleaned.get( idx );
                int[] normalized = Cleaner.normalize( line );

                // print out

                              for ( int idy = 0; idy < normalized.length; idy++ )
                                System.out.print( normalized[ idy ] + " " );
                              System.out.println();
             
            }
           
        }
        catch ( FileNotFoundException e )
        {
            e.printStackTrace(  );
        }
    }
}
Avatar of sudhakar_koundinya
sudhakar_koundinya

Your code should be like this


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Cleaner
{
    //~ Static fields/initializers ---------------------------------------------------------------------------------------------------------

    static final int NUM_COLS = 17;

    //~ Methods ----------------------------------------------------------------------------------------------------------------------------

    public ArrayList readFile( String fileName ) throws java.io.FileNotFoundException
    {
        ArrayList ret = new ArrayList(  );
        try
        {
            BufferedReader fos = new BufferedReader( new FileReader( fileName ) );

            String         line = fos.readLine(  );
            while ( line != null )
            {
                String[] array   = line.split( "," );
                int[]    numbers = new int[ NUM_COLS ];
                for ( int idx = 0; idx < NUM_COLS; idx++ )
                {
            try
            {
                          numbers[ idx ] = Integer.parseInt( array[ idx ] );
            }
            cathc(Exception ex)
            {
                  numbers[idx]=-1;
            }
                }

                line = fos.readLine(  );
                ret.add( numbers );
            }

            fos.close(  );
        }
        catch ( IOException ex )
        {
            System.out.println( ex );
        }

        return ret;
    }

    static boolean delete( int[] listOfIntegers )
    {
        boolean delete = false;
        Arrays.sort( listOfIntegers );

        int median      = listOfIntegers[ listOfIntegers.length / 2 ];
        int targetValue = median * 3;

        for ( int i = 0; ( i < listOfIntegers.length ) && ( !delete ); i++ )
        {
            int testValue = listOfIntegers[ i ];
            delete = testValue > targetValue;
            System.out.println( "test=" + testValue + ", target=" + targetValue );
        }

        return delete;
    }

    private static int min( int[] list )
    {
        int min = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] < min )
            {
                min = list[ idx ];
            }
        }

        return min;
    }

    private static int max( int[] list )
    {
        int max = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] > max )
            {
                max = list[ idx ];
            }
        }

        return max;
    }

    static int[] normalize( int[] list )
    {
        int[] normalized  = new int[ list.length ];
        int   min         = min( normalized );
        int   maxMinusMin = max( normalized ) - min;

        for ( int i = 0; ( i < list.length ); i++ )
        {
            int s = list[ i ];

            // calculate
            int calculated = ( s - min ) / maxMinusMin;
            normalized[ i ] = calculated;
        }

        return normalized;
    }

    public ArrayList cleaning( ArrayList list )
    {
        for ( int idx = 0; idx < list.size(  ); idx++ )
        {
            int[] line = (int[])list.get( idx );
            if ( delete( line ) )
            {
                list.remove( idx );
            }
        }

        return list;
    }

    public static void main( String[] args )
    {
        /*if ( args.length == 0 )
        {
            throw new IllegalArgumentException( "Usage: Cleaner filename " );
        }*/

        Cleaner   cleaner = new Cleaner();

        ArrayList lines;
        try
        {
            //lines = cleaner.readFile( args[ 0 ] );
            lines = cleaner.readFile( "testFile" );

            ArrayList cleaned = cleaner.cleaning( lines );

            for ( int idx = 0; idx < cleaned.size(  ); idx++ )
            {
                int[] line       = (int[])cleaned.get( idx );
                int[] normalized = Cleaner.normalize( line );

                // print out

                         for ( int idy = 0; idy < normalized.length; idy++ )
                           System.out.print( normalized[ idy ] + " " );
                         System.out.println();
             
            }
           
        }
        catch ( FileNotFoundException e )
        {
            e.printStackTrace(  );
        }
    }
}
ignore previous one

ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmccy

ASKER

ok let me try it first !! thanks !
Avatar of mmccy

ASKER

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Cleaner
{
    //~ Static fields/initializers ---------------------------------------------------------------------------------------------------------

    static final int NUM_COLS = 17;

    //~ Methods ----------------------------------------------------------------------------------------------------------------------------

    public ArrayList readFile( String fileName ) throws java.io.FileNotFoundException
    {
        ArrayList ret = new ArrayList(  );
        try
        {
            BufferedReader fos = new BufferedReader( new FileReader( fileName ) );

            String         line = fos.readLine(  );
            while ( line != null )
            {
                String[] array   = line.split( "," );
                int[]    numbers = new int[ NUM_COLS ];
                for ( int idx = 0; idx < NUM_COLS; idx++ )
                {
          try
          {
                         numbers[ idx ] = Integer.parseInt( array[ idx ]);//.trim().length()>0? array[ idx ].trim():"-1" );
                         
          }
          catch(Exception ex)
          {
               System.out.println("Error - Wrong number at column: " + idx + " text is: " + array[ idx ] );
               numbers[idx]=-1;
          }
                }

                line = fos.readLine(  );
                ret.add( numbers );
            }

            fos.close(  );
        }
        catch ( IOException ex )
        {
            System.out.println( ex );
        }

        return ret;
    }

    static boolean delete( int[] listOfIntegers )
    {
        boolean delete = false;
        Arrays.sort( listOfIntegers );

        int median      = listOfIntegers[ listOfIntegers.length / 2 ];
        int targetValue = median * 3;

        for ( int i = 0; ( i < listOfIntegers.length ) && ( !delete ); i++ )
        {
            int testValue = listOfIntegers[ i ];
            delete = testValue > targetValue;
            System.out.println( "test=" + testValue + ", target=" + targetValue );
        }

        return delete;
    }

    private static int min( int[] list )
    {
        int min = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] < min )
            {
                min = list[ idx ];
            }
        }

        return min;
    }

    private static int max( int[] list )
    {
        int max = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] > max )
            {
                max = list[ idx ];
            }
        }

        return max;
    }

    static int[] normalize( int[] list )
    {
        int[] normalized  = new int[ list.length ];
        int   min         = min( normalized );
        int   maxMinusMin = max( normalized ) - min;

        for ( int i = 0; ( i < list.length ); i++ )
        {
            int s = list[ i ];

            // calculate
            int calculated = ( s - min ) / maxMinusMin;
            normalized[ i ] = calculated;
        }

        return normalized;
    }

    public ArrayList cleaning( ArrayList list )
    {
        for ( int idx = 0; idx < list.size(  ); idx++ )
        {
            int[] line = (int[])list.get( idx );
            if ( delete( line ) )
            {
                list.remove( idx );
            }
        }

        return list;
    }

    public static void main( String[] args )
    {
        /*if ( args.length == 0 )
        {
            throw new IllegalArgumentException( "Usage: Cleaner filename " );
        }*/

        Cleaner   cleaner = new Cleaner();

        ArrayList lines;
        try
        {
            //lines = cleaner.readFile( args[ 0 ] );
            lines = cleaner.readFile( "testFile" );

            ArrayList cleaned = cleaner.cleaning( lines );

            for ( int idx = 0; idx < cleaned.size(  ); idx++ )
            {
                int[] line       = (int[])cleaned.get( idx );
                int[] normalized = Cleaner.normalize( line );

                // print out

                         for ( int idy = 0; idy < normalized.length; idy++ )
                           System.out.print( normalized[ idy ] + " " );
                         System.out.println();
             
            }
           
        }
        catch ( FileNotFoundException e )
        {
            e.printStackTrace(  );
        }
    }
}

I use the above code and has the errors belows:

C:\Sun\AppServer\jdk\bin>java Cleaner
Error - Wrong number at column: 0 text is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at Cleaner.readFile(Cleaner.java:38)
        at Cleaner.main(Cleaner.java:150)

C:\Sun\AppServer\jdk\bin>
Avatar of mmccy

ASKER

try
          {
                         numbers[ idx ] = Integer.parseInt( array[ idx ].trim().length()>0? array[ idx ].trim():"-1" );
                         
          }
When I use this provided by sudhakar_koundinya, the errors are:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at Cleaner.readFile(Cleaner.java:38)
        at Cleaner.main(Cleaner.java:150)
Press any key to continue...
Avatar of mmccy

ASKER

would u(both experts) mind I email the text file to u all for running and testing ??
Avatar of CEHJ
mmccy i gave you some code for this yesterday - it still seems to be an ongoing problem. Let's tackle it another way:

a. show us some sample data
b. show us what final result you're expecting
Avatar of mmccy

ASKER

oh !! CEHJ !!! nice to meet u again !!! yes I really got problem in the code u give me !! I can not integrate them together !!
Also your code is much advanced and I find the code of JugglerW is easier for me to understand !! So I choose that for a try !!
below is the sample data in the text file :

321,371,361,305,112,88,109,89,115,97,101,91,922,468,586,4333,415047,1
317,370,357,294,113,87,110,89,110,93,107,92,924,468,591,4391,419170,1
306,362,348,265,114,88,110,90,110,91,103,81,908,475,581,4248,416936,1
..............
320,378,361,298,113,89,109,90,113,93,105,90,926,469,590,4478,426774,1
346,387,349,278,92,80,95,87,86,78,78,70,868,428,538,4279,365147,2
335,382,349,277,93,84,93,85,87,78,78,69,871,432,542,4241,367027,2
...................
347,388,349,286,93,81,95,85,88,80,83,74,861,427,532,4294,358412,2
339,402,378,286,92,79,93,90,99,87,78,74,890,451,529,4427,387917,3
334,401,381,290,93,78,93,89,98,87,79,75,890,450,527,4443,386559,3
335,396,380,294,91,79,91,90,89,83,81,75,901,448,538,4468,389894,3
...............
333,376,339,258,84,74,86,76,90,68,69,62,765,391,445,3886,291724,100
336,386,351,275,87,71,85,72,84,69,75,64,787,392,455,3800,295657,100
329,377,350,276,86,72,89,72,91,72,80,67,777,391,448,3863,291137,100

the last column indicated the userid and the first 16 of each row is the user's sample, basically no need to deal with the last column and treat it as a userid for indication is ok !!!!
what I expected to do is to readin the file and outpout a cleaned and normalised 2D Array(ArraylistofArraylist or Arraylist of int[]) !!After that the normalized list will be used for other very complicated calculation which I will ask again after the normalised list is working out !!
It makes me crazy for this data !!!

Deeply thanks for all of you !!


come on people, rather than fix the code should you be explaining why mmccy is getting these errors?

mmccy, numberformatexception is the result of passing/assinging an incorrect value to whats being expected eg  

int myint = youstring  !!!!!!

decalre int expecting in but assigned STRING!

it can also apply to floats,chars,doubles etc

JugglerW's example is one (and a good one) way of catching numberformat exception

          try
          {
// if anything other than an int is returned by Integer.parseInt() the exception is thrown
                         int myint = Integer.parseInt([string]));
          }
          catch(Exception ex)
          {
               System.out.println("ooooooops, numberformatexception " + ex.toString());
          }
 
re arrayindexoutofbound exception, you are trying to access an element in an array that doesnt exist.

ie

int array[] = new int[10];

in the above you have created an int array to hold 10 and ONLY 10 int elements.  NOTE: it has not been initialized so any attempt to access array[0] would result in an error

to initialize the array you can :

int array[] = {1,2,3,4,5,6,7,8,9,10};

or

int array[] = new int[10];

array[0] = 99;
array[1] = 12;
 etc...

ps, you will learn nothing if you just ask people to solve your problems!

"give a man fish, he eats for a day, TEACH a man to fish, he eats for a LIFETIME"!
Avatar of mmccy

ASKER

To JohnnyAffa,

Sorry, Firstly I am not the one u say in the last sentence of your last post !! I try to understand and search the JDK doc after I got the code from any experts.
I am now understanding the practical usage of each functions and methods !!

numbers[ idx ] = Integer.parseInt( array[ idx ]);
to my understanding, the code is correct , converting a string into an int using Integer.parseInt(string),

also the initialization problem,
static final int NUM_COLS = 17;
int[]    numbers = new int[ NUM_COLS ];
I 've got 17 column in one row so int[17] and looping for readin unknown rows, so Arraylist of int[] ,

Would u mind to show your corrections for me ? I really find the logics and syntax is ok in the program !! but dont know why have such errors !!

Thank you !
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmccy

ASKER

oh !! I see !!
now I am trying my best to debug and hope that I can do it before midnight !!!

Thanks alot !!
@JohnnyAffa:
The NUM_COLS constant was important in this case because only the first 17 (or 16?) columns of the data should be used. But you are right when the data is filled in the array you should use array.length further on.

For the ArrayIndexOutOfBoundsEx. I assume that the split() method returns fewer than NUM_COLS items, perhaps there are empty lines or the last line is empty. Would use:

                String[] array   = line.split( "," );
                if ( array.length >= NUM_COLS )
                {
                    int[]    numbers = new int[ NUM_COLS ];
                    for ( int idx = 0; idx < NUM_COLS; idx++ )
                    ...
                }


 
Avatar of mmccy

ASKER

the error of Arrayoutofindexboundary is caused by

//System.out.println("Error - Wrong number at column: " + idx + " text is: " + array[ idx ] );
//numbers[idx]=-1;

I comment these two rows and the program can run without errors, although, I finally can read in the file and do the calculation, there are many strange results comes out !!! also the display is one column only !! I wan to show them in 17 columns !! I am still working on it !! Hope that experts can still give me a hand to finish this task !! Below are the code in the program, thanks

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

public class Cleaner
{
    //~ Methods ----------------------------------------------------------------------------------------------------------------------------

    public ArrayList readFile( String fileName ) throws java.io.FileNotFoundException
    {
        ArrayList ret = new ArrayList();
        try
        {
            BufferedReader fos = new BufferedReader( new FileReader( fileName ));
            String[] array=new String[17];
            String   line = fos.readLine();
            while ( line != null )
            {
                array = line.split( "," );
                float[] numbers = new float[17];
                for ( int idx = 0; idx <numbers.length ; idx++ )
                {
          try
          {
               numbers[ idx ] = Integer.parseInt( array[ idx ].trim().length()>0? array[ idx ].trim():"-1", 10 );
                         
          }
          catch(Exception ex)
          {
               //System.out.println("Error - Wrong number at column: " + idx + " text is: " + array[ idx ] );
               //numbers[idx]=-1;
          }
         
                }

                line = fos.readLine();
                ret.add( numbers );
            }

            fos.close(  );
        }
        catch ( IOException ex )
        {
            System.out.println( ex );
        }

        return ret;
    }    


    static boolean delete( float[] listOfIntegers )
    {
        boolean delete = false;
        Arrays.sort( listOfIntegers );

        float median      = listOfIntegers[ listOfIntegers.length / 2 ];
        float targetValue = median * 3;

        for ( int i = 0; ( i < listOfIntegers.length ) && ( !delete ); i++ )
        {
            float testValue = listOfIntegers[ i ];
            delete = testValue > targetValue;
            System.out.println( "test=" + testValue + ", target=" + targetValue );
        }

        return delete;
    }

    private static float min( float[] list )
    {
        float min = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] < min )
            {
                min = list[ idx ];
            }
        }

        return min;
    }

    private static float max( float[] list )
    {
        float max = list[ 0 ];

        for ( int idx = 1; idx < list.length; idx++ )
        {
            if ( list[ idx ] > max )
            {
                max = list[ idx ];
            }
        }

        return max;
    }

    static float[] normalize( float[] list )
    {
        float[] normalized  = new float[ list.length ];
        float   min         = min( normalized );
        float   maxMinusMin = max( normalized ) - min;

        for ( int i = 0; ( i < list.length ); i++ )
        {
            float s = list[ i ];

            // calculate
            float calculated = ( s - min ) / maxMinusMin;
            normalized[ i ] = calculated;
        }

        return normalized;
    }

    public ArrayList cleaning( ArrayList list )
    {
        for ( int idx = 0; idx < list.size(  ); idx++ )
        {
            float[] line = (float[])list.get( idx );
            if ( delete( line ) )
            {
                list.remove( idx );
            }
        }

        return list;
    }

    public static void main( String args[] )
    {
        Cleaner   cleaner = new Cleaner();

        ArrayList lines;
        try
        {
            //lines = cleaner.readFile( args[ 0 ] );
            lines = cleaner.readFile("testFile");
            for ( int idx = 0; idx < lines.size(  ); idx++ )
            {
                float[] line       = (float[])lines.get( idx );
                               
                         for ( int idy = 0; idy < line.length; idy++ )
                           {System.out.print( line[ idy ] + " " );
                         System.out.println();
                         }
                         
             }

            /*ArrayList cleaned = cleaner.cleaning( lines );
           
            for ( int idx = 0; idx < lines.size(  ); idx++ )
            {
                float[] line       = (float[])lines.get( idx );
                float[] normalized = cleaner.normalize( line );

                // print out

                         for ( int idy = 0; idy < normalized.length; idy++ )
                           {System.out.print( normalized[ idy ] + " " );
                         System.out.println();
                         }
                         /*for ( int idy = 0; idy < line.length; idy++ )
                           System.out.print( line[ idy ] + " " );
                         System.out.println();*/
             
            }
           
       
        catch ( FileNotFoundException e )
        {
            e.printStackTrace(  );
        }
    }
}
Avatar of mmccy

ASKER

at this stage, I just println out what is read from the file only and still not using cleaning () and normalise() !!
Avatar of mmccy

ASKER

After I do the cleaning part , there are 499 lines are cut !! this is very strange !!!
Avatar of mmccy

ASKER

I should not include the last column in the cleaning part because it is an userid !!
>>
normalized data(each cell)=[s - min(S)]/[max(S)-min(S)]
where s is the original data, min(S) is the mininium value of the each column and max(S) is the maximium value of each column !!
>>

It would appear that the minimum/maximum value of each row is being calculated, not each column ...
Avatar of mmccy

ASKER

yes !! I also find this problem and trying to fix it , but I am afraid I dont know have enough time !
for the cleaning part , it seems also doing for each row not for each column !!
juggler, whats wrong with [array].length() -1?
@JohnnyAffa:

>juggler, whats wrong with [array].length() -1?

Nothings wrong with it at all, but I thought because the problem to solve gives a fixed number ("use the first N columns") it would be good style to use a constant here.
If the then, while reading, the input does not met the column size for any reason there should  an exception thrown and I could fix my input.
After reading the file I want to be sure there are exactly NUM_COLS in my int arrays for every line of file because the calculations following will depend on this.

CU JugglerW
>>for the cleaning part , it seems also doing for each row not for each column !!

Well, according to your earlier spec, this *should* be happening per row, not per column:

>>if the cell contains a data which is > median x 3 , then the whole ROW of the data sample will be deleted
Avatar of mmccy

ASKER

yes exactly !! delete the whole row if the data(one cell) is greater than the medain of each column x 3 !
This has all got a bit messy now. I suggest you do the following:

a. open a new question doing the following:
b. post the definitions again
c. post 2 or 3 rows of sample data, showing the expected result
Avatar of mmccy

ASKER

ok ! posted ! thanks !
Going over now...