DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2005
    Posts
    2

    Returning a multidimensional array from a method in a jsp

    Hi,
    I was 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, "\n");
    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;
    }
    %>

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    Declare importArray outside the try block then it should work

  3. #3
    Join Date
    Dec 2005
    Posts
    2
    Thank you
    declaring and initializing outside the try worked.
    Casey

Similar Threads

  1. Replies: 2
    Last Post: 11-10-2005, 01:33 PM
  2. Return an array from a class method
    By nubira in forum C++
    Replies: 5
    Last Post: 11-04-2005, 02:53 PM
  3. Replies: 2
    Last Post: 04-15-2005, 09:06 PM
  4. Replies: 4
    Last Post: 03-29-2005, 06:26 PM
  5. Replies: 2
    Last Post: 03-03-2001, 11:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links