CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Java IO Problem

  1. #1
    Join Date
    Aug 2009
    Posts
    52

    Java IO Problem

    Hi Guys I have a textfile in the following format, the integer value before the ":" delimeter states the column where the value after the ":" should be stored in a 2D array. For Eg: 21:1.00, this should go into the 21st column of the array with the value 1.00. Therefore if you print out array[0][21] or array[1][9] it should give you the value of 1.00. Column numbers not stated in the textfile have a value of 0 for eg: array[0][0] to array[0][20] all will have values initialised to 0. So for each line it will read its column and value and store it into the correct array[][] parameters. How do i go about doing it?? Any help is appreciated thank you

    Example of TextFile
    21:1.00 42:1.00 63:1.00 84:1.00 105:1.00
    9:1.00 32:0.50 41:0.50 44:1.00
    3:1.00 5:0.50 8:0.50 15:1.00
    .........
    ....

    Code so far:
    public static void ReadFile2String(String InputFile) {
    String line = " ";
    int storeArray[][] = new int[1700][357];//1700 rows and 357 columns each row
    try {
    FileReader fr = new FileReader(InputFile);
    BufferedReader br = new BufferedReader(fr);
    while ((line = br.readLine()) != null) //loop through each line
    {

    StringTokenizer st = new StringTokenizer(line, ":");

    while (st.hasMoreTokens()) {
    String s = st.nextToken();
    System.out.println(s);
    }

    }


    } catch (Exception e) {
    System.out.println(e);
    }
    }

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Java IO Problem

    You forgot to use [code] tags.

    Avoid using StringTokenizer, it is not deprecated but its use is discouraged. Use the String.split() method instead.

    In your case you will have to use it twice: first to split the line read from the file at the spaces between the column/value pairs, and then to split each of these at the :.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured