0

I want to get a 2 Dimensional Array's position in java.

Purpose: I get this 2 dimensional array from an CSV file for a validation. So I want to identify which one is invalid by mentioning its Cell position.

Example:

    A   B   C   D    E  

1  a   b    c   d    e

2  f    g    h    i     j

3  k    l    m   n    o

4  p   q    k    r    s

If I give 'h' in the a code with the above input, it should give me C2. API for this will be very helpful.

Can anyone help me in this please?

Harish Raj
  • 1,565
  • 3
  • 17
  • 27

2 Answers2

0

Apache POI should help you for excel manipulation

Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29
0

Use the following method for better result.

private static String ConvertColumnNumberToChars( int i ){

if( i < 0 )
throw new UnsupportedOperationException("Converted number must be greater than zero.");

int iBase = 'Z' - 'A'+1;
if( iBase > Character.MAX_RADIX )
throw new UnsupportedOperationException("This JRE can't convert to radix greater than "+Character.MAX_RADIX);

String interConversion = Integer.toString(i-1, iBase).toUpperCase();

//System.out.print("inter: "+ interConversion +";  ");

char[] ac = interConversion.toCharArray();

for( int j = 0; j < ac.length; j++ ) {

int poziceOdzadu = ac.length - j - 1;
char c = ac[j];
ac[j] =  (char) ('A' - poziceOdzadu + Character.digit( c, iBase ));

}
return String.copyValueOf( ac );

}

All you gotta do is to input an int value to this method which want to change to the column name.

String Value's Row Number+1 is the Row Number of the CSV file and ConvertColumnNumberToChars(Number+1) is the column character name.

Good Luck!

Harish Raj
  • 1,565
  • 3
  • 17
  • 27