returning a multidimensional array

Hi,
Iwas wondering if anyone could tell me how to return a 2d array from a method im using in a jsp page (im not sure how this will differ from other java programming) I'll put my code below if that helps.
I get the error located on the return statement line at the bottom that "importArray cannot be resolved" and if I add brackets to the importArray on the return statement "return importArray[][];" I get anther error that I should add a ";" to complete the return statement.
Any help would be appreciated.
thanks
Casey

<%!
static String[][] readCSV(File importFile)
{
String fileContents = "";
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
int linecount = 0;

try
{
input = new BufferedReader (new FileReader(importFile));
String line = null;
while((line = input.readLine()) != null)
{
contents.append(line);
contents.append(System.getProperty("line.separator"));
}

if (input != null)
{
input.close();
}

fileContents = contents.toString();
StringTokenizer row = new StringTokenizer(fileContents, "
");
int rowNum = row.countTokens();
String nextRow = row.nextToken();
StringTokenizer col = new StringTokenizer(nextRow, ",");
int colNum = col.countTokens();
String[][] importArray;
importArray = new String[rowNum][colNum];

rowNum = 0;
colNum = 0;
while(row.hasMoreTokens())
{
StringTokenizer field = new StringTokenizer(nextRow, ",");
while(field.hasMoreTokens())
{
String nextField = field.nextToken();
importArray[rowNum][colNum] = nextField;
colNum++;
}
nextRow = row.nextToken();
rowNum++;
colNum = 0;
}
rowNum = 0;
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
return importArray;
}
%>

Comments

  • As [italic]importArray[/italic] is defined inside the try-catch block it is not known outside the block.
    Solution, move this:
    [CODE][SIZE=2]
    }
    catch (Exception e)
    {
    System.out.println("Exception: " + e.getMessage());
    }
    [/SIZE][/CODE]
    up here
    [CODE][SIZE=2]
    if (input != null) {
    input.close();
    }
    [green]
    }
    catch (Exception e)
    {
    System.out.println("Exception: " + e.getMessage());
    }
    [/green]
    fileContents = contents.toString();
    [/SIZE][/CODE]
    as the [italic]StringTokenizer[/italic] does not need a try-catch block
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion