Solved

Can not run this program !!

Posted on 2004-07-31
28
290 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(  );
        }
    }
}
0
Comment
Question by:mmccy
  • 14
  • 5
  • 3
  • +2
28 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11686963
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(  );
        }
    }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11686966
ignore previous one

0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 200 total points
ID: 11686968
should be 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 ].trim().length()>0? array[ idx ].trim():"-1" );
            }
            catch(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(  );
        }
    }
}
0
 
LVL 7

Assisted Solution

by:JugglerW
JugglerW earned 200 total points
ID: 11686974
change this:

          try
          {
                         numbers[ idx ] = Integer.parseInt( array[ idx ] );
          }
          cathc(Exception ex)
          {
               numbers[idx]=-1;
          }

to:

          try
          {
                         numbers[ idx ] = Integer.parseInt( array[ idx ] );
          }
          catch(Exception ex)
          {
               System.out.println("Error - Wrong number at column: " + idx + " text is: " + array[ idx ] );
               numbers[idx]=-1;
          }

If you add an int to count the lines of the input file you may print also the line number where the error occurred.
I think there must be something wrong with your input file because the exception oly occurs if parseInt() gets no number.
0
 

Author Comment

by:mmccy
ID: 11687068
ok let me try it first !! thanks !
0
 

Author Comment

by:mmccy
ID: 11687079
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>
0
 

Author Comment

by:mmccy
ID: 11687088
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...
0
 

Author Comment

by:mmccy
ID: 11687103
would u(both experts) mind I email the text file to u all for running and testing ??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11687107
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
0
 

Author Comment

by:mmccy
ID: 11687126
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 !!


0
 
LVL 3

Expert Comment

by:JohnnyAffa
ID: 11687133
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"!
0
 

Author Comment

by:mmccy
ID: 11687194
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 !
0
 
LVL 3

Assisted Solution

by:JohnnyAffa
JohnnyAffa earned 100 total points
ID: 11687459
mmccy,

1) my comment wasnt directed towards you

2) you dont need the static varaible NUM_COLS, you can access the size using numbers.length(). NEVER ever use a varaible to store the size of an array when [array].length() is available (proper/better)

3) the only time you should use an ArrayList is when you dont know what the size of the array is or when you want to manipulate the data (sorting,finding etc)  NOTE: theres is overhead associated ArrayList,Vector data structures

4) once you create and array POPULATE it before using it, which is probably why youre getting arratindexoutofbounds errors
0
 

Author Comment

by:mmccy
ID: 11687469
oh !! I see !!
now I am trying my best to debug and hope that I can do it before midnight !!!

Thanks alot !!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:JugglerW
ID: 11688052
@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++ )
                    ...
                }


 
0
 

Author Comment

by:mmccy
ID: 11688094
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(  );
        }
    }
}
0
 

Author Comment

by:mmccy
ID: 11688141
at this stage, I just println out what is read from the file only and still not using cleaning () and normalise() !!
0
 

Author Comment

by:mmccy
ID: 11688590
After I do the cleaning part , there are 499 lines are cut !! this is very strange !!!
0
 

Author Comment

by:mmccy
ID: 11688607
I should not include the last column in the cleaning part because it is an userid !!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11689086
>>
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 ...
0
 

Author Comment

by:mmccy
ID: 11691150
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 !!
0
 
LVL 3

Expert Comment

by:JohnnyAffa
ID: 11691788
juggler, whats wrong with [array].length() -1?
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11692396
@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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11692468
>>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
0
 

Author Comment

by:mmccy
ID: 11692918
yes exactly !! delete the whole row if the data(one cell) is greater than the medain of each column x 3 !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11692954
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
0
 

Author Comment

by:mmccy
ID: 11693512
ok ! posted ! thanks !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11693525
Going over now...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
noX challenge 17 76
Saving Tweets with emojis using Twitter4J in a Mysql table 2 52
Python Assistance 7 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now