Results 1 to 12 of 12
- 01-21-2009, 05:16 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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:
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. ThanksJava Code:for(int i = 0; i < 10; i++) { for(int j = 0; j < 6; j++) { list[i][j] = input.readLine(); } }
- 01-21-2009, 05:36 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, tel me what's the way you are going to do this? From where you start?
- 01-21-2009, 05:59 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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
- 01-21-2009, 06:00 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all you have to read the file line by line. Can you do that, if so how did you do that?
- 01-21-2009, 06:06 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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
*website edited out for privacy*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(); } }
This only works if text file is
1
2
3
4
etc
- 01-21-2009, 06:10 AM #6
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)
- 01-21-2009, 06:13 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-21-2009, 06:14 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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.
- 01-21-2009, 06:15 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Once you read it as a string, later it's possible to convert it into any type. Seems you are worrying about that.
- 01-21-2009, 07:09 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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.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(); } }Last edited by Bomber_Will; 01-21-2009 at 07:13 AM.
- 01-21-2009, 07:57 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What happen when you run this code?
- 01-21-2009, 08:19 AM #12
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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:
Where choiceInt is a number the user picks when the program is ran. After that I have the code: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 firstText, secondText, etc send the values to TextField boxes in my appletJava Code:firstText.setText(first); secondText.setText(second); thirdText.setText(third); fourthText.setText(fourth); fifthText.setText(fifth); sixthText.setText(sixth);
Last edited by Bomber_Will; 01-21-2009 at 08:24 AM.
Similar Threads
-
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
simplest way to make 2d array to string
By Tamu in forum Advanced JavaReplies: 4Last Post: 11-25-2008, 04:50 PM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks