Results 1 to 10 of 10
  1. #1
    newToIt is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default reading in unsigned ints into a 2D array

    trying to read in 100 x 100 integers from a file into a 2 dimensional array. using a buffered reader and parsing the data from the file from string type to int type and having muchos problems, code as follows (i get zilch)

    FileReader fileReader = new FileReader ("myMaxSum.txt");
    BufferedReader reader = new BufferedReader (fileReader);

    String line = reader.readLine();

    while( line != null ){
    StringTokenizer tokens = new StringTokenizer(line);
    while (tokens.hasMoreTokens() ){

    String s = tokens.nextToken();
    int x = Integer.parseInt(s);
    for ( i=0; i <= row-1; i++ ){
    for ( j = 0; j <= col-1; j++ ){
    matrix[i][j] = x;
    }
    }

    }
    line = reader.readLine();
    }
    reader.close();
    }
    catch (IOException e){
    e.printStackTrace();
    }

    for ( i=0; i <= row-1; i++ ){
    for ( j = 0; j <= col-1; j++ ){
    System.out.print(matrix[i][j]+ " ");
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It might help to know what problems, and on exactly which lines.

  3. #3
    newToIt is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    howdy,

    i run it, get no errors but i should see some output and do not -- i get zilch, nothing no errors no output???

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you explain more clearly what's your question is? You wont get the expected values? Did you debug the code and see? Basically think about the logic you use and data arrangement in the text file.

    And also please use code tags when you posting code segments again. Formatted codes are easy to read.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Where do variables row and col get initialized?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I think he post just a segment from the full class here. ;) But it's mess to go through is without knowing initial values, isn't it? More, I'm mess on how data arrange on the text file.

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Looking at your code, I get the impression you haven't fully thought through what you want to get done.

    Every time you get a new int, you run through all your rows and columns, sticking the new int into every array entry. That doesn't make much sense.

    Here's my suggestion; it's what I still do when I hit a patch of logic I don't fully understand.

    Take several pieces of a paper and a pencil.

    Write down the steps you want accomplished, in *plain* language. Use indentation to help you see any loops or conditions.

    Read through what you wrote down. If it doesn't make good sense, do it again.

    Writing Java code when you don't understand the logic adds a huge layer of complexity, which guarantees that both the logic and the code will be wrong. A picture is worth a thousand words, and a sentence in plain language is worth a thousand lines of code.

  8. #8
    svpriyan is offline Member
    Join Date
    Mar 2009
    Location
    London
    Posts
    8
    Rep Power
    0

    Default Need a Help in reading a 2D array in Java

    Hello Colleagues,
    I am having a relation with TIDs, ITEM IDs and in a file it appears in the following format

    TID ITEM
    1 1
    1 2
    1 3
    2 1
    3 4
    3 5
    3 6

    Now, I am intended to change that into Market Basket Data Format which might look like

    TID ITEM
    1 1 2 3
    2 1
    3 4 5 6
    4 1 8

    I wanna wanted to use Java 2D array to use to read the previous content and write to a new file like the above format.
    Could any one help me on this

    Thanks
    Priyan

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you explain your question more clearly. How did you convert data into the new arrangement? And also what you have tried so far? Basically you have to arrange data in new format and write them to a text file.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Priya... comment... it would probably be better to place your own post about your problem... that way you won't anger the original poster and your post will get more focus...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 12:51 AM
  2. Array not reading. Plz help
    By cmizer in forum New To Java
    Replies: 14
    Last Post: 02-12-2009, 12:02 AM
  3. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 08:19 AM
  4. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-18-2008, 11:51 PM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

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