Results 1 to 12 of 12
  1. #1
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default Reading from a file to make an array

    Can someone give me a way that file1:

    1 2 3 4 5 6
    2 3 4 5 6 7
    ...etc

    would be treated like file2

    1
    2
    3
    4
    5
    6
    2
    3
    4
    etc..

    I want to do something like list[0][0] = 1, list[0][1] = 2, etc

    This piece of code only works for file2:

    Java Code:
    for(int i = 0; i < 10; i++)
    {
                	for(int j = 0; j < 6; j++)
                	{
                		list[i][j] = input.readLine();
                	}
    }
    I was looking into String Tokenizer but I haven't found any code yet that can help. I'll keep looking though while this topic is open. Thanks

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

  3. #3
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    I don't know how am I going to do this yet.

    I just need a code that will assign 1 on list[0][0], 2 on list[0][1], 3 on list[0][2]...2 on list[1][0], 3 on list list[1][1], etc when the text file is

    1 2 3 4 5 6
    2 3 4 5 6 7

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

    Default

    First of all you have to read the file line by line. Can you do that, if so how did you do that?

  5. #5
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Yeah I can do that but it reads 1 2 3 4 5 6 as 1 2 3 4 5 6 instead of being 1 alone and then 2 alone

    Java Code:
    URL site = new URL("http://www.website.com/matrix.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader(site.openStream()));
    
    for(int i = 0; i < 10; i++)
    {
                	for(int j = 0; j < 6; j++)
                	{
                		list[i][j] = input.readLine();
                	}
    }
    *website edited out for privacy*

    This only works if text file is

    1
    2
    3
    4
    etc

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    why don't u just read file in as string. then use charAt(int index) to print the char's out one by one.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

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

    Default

    Yes as angryboy says, read each line as a string. Easy to manipulate. And also if you want you can use a regular expression as well.

  8. #8
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Well this program I'm writing has the user pick a number and then the program go to list[number - 1] and displays everything on that line. Each line contains 6 values that needs to be displayed on different parts of the screen. (Applet with text boxes)

    Edit: I'll try to do it that way right now. I'll post in like 5-10 minutes to update you all.

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

    Default

    Once you read it as a string, later it's possible to convert it into any type. Seems you are worrying about that.

  10. #10
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Java Code:
    URL site = new URL("http://www.website.com/matrix.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader(site.openStream()));
                
                for(int i = 0; i < 10; i++)
                {
                		list[i] = input.readLine();
                }     
               
                for(int i = 0; i < 10; i++)
                {
                	String number = list[i];
                	StringTokenizer st = new StringTokenizer(number);
                	
                	for(int j = 0; j < 6; j++)
                	{
                			stats[i][j] = st.nextToken();
                	}
                }
    This seems to work even though I had to use 2 arrays. I can't use charAt(int) since the text file is going to contain double digit numbers, sorry for not mentioning it earlier. Thanks for giving me the idea of treating each line as a string and then breaking it down from there. Eranga, I was more worried about complicating such a simple code, but I guess this will have to do.
    Last edited by Bomber_Will; 01-21-2009 at 08:13 AM.

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

  12. #12
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    lets say you have the file

    1 2 3 4 5 6
    2 3 4 5 6 7

    The first for loop would give

    list[0]: 1 2 3 4 5 6
    list[1]: 2 3 4 5 6 7

    The second for loop would give

    stats[0][0]: 1
    stats[0][1]: 2
    stats[0][2]: 3
    stats[0][3]: 4
    stats[0][4]: 5
    stats[0][5]: 6

    stats[1][0]: 2
    stats[1][1]: 3
    stats[1][2]: 4
    stats[1][3]: 5
    stats[1][4]: 6
    stats[1][5]: 7

    Then I have:

    Java Code:
    String first = stats[choiceInt - 1][0];
    String second = stats[choiceInt - 1][1];
    String third = stats[choiceInt - 1][2];
    String fourth = stats[choiceInt - 1][3];
    String fifth = stats[choiceInt - 1][4];
    String sixth = stats[choiceInt - 1][5];
    Where choiceInt is a number the user picks when the program is ran. After that I have the code:

    Java Code:
    firstText.setText(first);
    secondText.setText(second);
    thirdText.setText(third);
    fourthText.setText(fourth);
    fifthText.setText(fifth);
    sixthText.setText(sixth);
    Where firstText, secondText, etc send the values to TextField boxes in my applet
    Last edited by Bomber_Will; 01-21-2009 at 09:24 AM.

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, 01:51 AM
  2. simplest way to make 2d array to string
    By Tamu in forum Advanced Java
    Replies: 4
    Last Post: 11-25-2008, 05:50 PM
  3. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM
  4. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11: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
  •