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.NumberFormatExce ption: For input string: ""
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.
java:48)
at java.lang.Integer.parseInt (Integer.j ava:489)
at java.lang.Integer.parseInt (Integer.j ava:518)
at Cleaner.readFile(Cleaner.j ava:31)
at Cleaner.main(Cleaner.java: 141)
Press any key to continue...
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
import java.io.BufferedReader;
import java.io.FileNotFoundExcept ion;
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.FileNotFoundExcept ion
{
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( );
}
}
}
Thank you so much !!!
Exception in thread "main" java.lang.NumberFormatExce
at java.lang.NumberFormatExce
java:48)
at java.lang.Integer.parseInt
at java.lang.Integer.parseInt
at Cleaner.readFile(Cleaner.j
at Cleaner.main(Cleaner.java:
Press any key to continue...
--------------------------
import java.io.BufferedReader;
import java.io.FileNotFoundExcept
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.FileNotFoundExcept
{
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( );
}
}
}
ignore previous one
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok let me try it first !! thanks !
ASKER
import java.io.BufferedReader;
import java.io.FileNotFoundExcept ion;
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.FileNotFoundExcept ion
{
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>j ava Cleaner
Error - Wrong number at column: 0 text is:
Exception in thread "main" java.lang.ArrayIndexOutOfB oundsExcep tion: 1
at Cleaner.readFile(Cleaner.j ava:38)
at Cleaner.main(Cleaner.java: 150)
C:\Sun\AppServer\jdk\bin>
import java.io.FileNotFoundExcept
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.FileNotFoundExcept
{
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>j
Error - Wrong number at column: 0 text is:
Exception in thread "main" java.lang.ArrayIndexOutOfB
at Cleaner.readFile(Cleaner.j
at Cleaner.main(Cleaner.java:
C:\Sun\AppServer\jdk\bin>
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.ArrayIndexOutOfB oundsExcep tion: 1
at Cleaner.readFile(Cleaner.j ava:38)
at Cleaner.main(Cleaner.java: 150)
Press any key to continue...
{
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.ArrayIndexOutOfB
at Cleaner.readFile(Cleaner.j
at Cleaner.main(Cleaner.java:
Press any key to continue...
ASKER
would u(both experts) mind I email the text file to u all for running and testing ??
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
a. show us some sample data
b. show us what final result you're expecting
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,92 2,468,586, 4333,41504 7,1
317,370,357,294,113,87,110 ,89,110,93 ,107,92,92 4,468,591, 4391,41917 0,1
306,362,348,265,114,88,110 ,90,110,91 ,103,81,90 8,475,581, 4248,41693 6,1
..............
320,378,361,298,113,89,109 ,90,113,93 ,105,90,92 6,469,590, 4478,42677 4,1
346,387,349,278,92,80,95,8 7,86,78,78 ,70,868,42 8,538,4279 ,365147,2
335,382,349,277,93,84,93,8 5,87,78,78 ,69,871,43 2,542,4241 ,367027,2
...................
347,388,349,286,93,81,95,8 5,88,80,83 ,74,861,42 7,532,4294 ,358412,2
339,402,378,286,92,79,93,9 0,99,87,78 ,74,890,45 1,529,4427 ,387917,3
334,401,381,290,93,78,93,8 9,98,87,79 ,75,890,45 0,527,4443 ,386559,3
335,396,380,294,91,79,91,9 0,89,83,81 ,75,901,44 8,538,4468 ,389894,3
...............
333,376,339,258,84,74,86,7 6,90,68,69 ,62,765,39 1,445,3886 ,291724,10 0
336,386,351,275,87,71,85,7 2,84,69,75 ,64,787,39 2,455,3800 ,295657,10 0
329,377,350,276,86,72,89,7 2,91,72,80 ,67,777,39 1,448,3863 ,291137,10 0
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 !!
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
317,370,357,294,113,87,110
306,362,348,265,114,88,110
..............
320,378,361,298,113,89,109
346,387,349,278,92,80,95,8
335,382,349,277,93,84,93,8
...................
347,388,349,286,93,81,95,8
339,402,378,286,92,79,93,9
334,401,381,290,93,78,93,8
335,396,380,294,91,79,91,9
...............
333,376,339,258,84,74,86,7
336,386,351,275,87,71,85,7
329,377,350,276,86,72,89,7
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
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("oooooo ops, 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"!
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("oooooo
}
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"!
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 !
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oh !! I see !!
now I am trying my best to debug and hope that I can do it before midnight !!!
Thanks alot !!
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++ )
...
}
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++ )
...
}
ASKER
the error of Arrayoutofindexboundary is caused by
//System.out.println("Erro r - 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.FileNotFoundExcept ion
{
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("Erro r - 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( );
}
}
}
//System.out.println("Erro
//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.FileNotFoundExcept
{
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("Erro
//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( );
}
}
}
ASKER
at this stage, I just println out what is read from the file only and still not using cleaning () and normalise() !!
ASKER
After I do the cleaning part , there are 499 lines are cut !! this is very strange !!!
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 ...
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 ...
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 !!
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
>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
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
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
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
ASKER
ok ! posted ! thanks !
Going over now...
import java.io.BufferedReader;
import java.io.FileNotFoundExcept
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.FileNotFoundExcept
{
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( );
}
}
}