We help IT Professionals succeed at work.
Get Started

Can not run this program !!

mmccy
mmccy asked
on
370 Views
Last Modified: 2012-06-21
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(  );
        }
    }
}
Comment
Watch Question
This problem has been solved!
Unlock 3 Answers and 28 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE