We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › reading numbers from txt file (HAPPY NEW YEAR!)
Page Index Toggle Pages: 1
reading numbers from txt file (HAPPY NEW YEAR!) (Read 1771 times)
reading numbers from txt file (HAPPY NEW YEAR!)
Dec 31st, 2009, 10:48pm
 
Hi, I 'm making a small game.
At this stage I am trying to draw the level.
So I want to read data from a file and turn them into the level.
the level is a grid, let's say 10 x 10.
So I would like the file to be somewhat like that, so I can easily edit it.

1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 0 0 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 1 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1

1 = wall, 0 = ground.
Catch my drift?

All I need is some direction as to which command to use to read numbers from a .txt file and pass them to a variable. But the separator of numbers should be either " " or ENTER.
Is there any way to do that?
(Perhaps with the LoadBytes() command?)
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #1 - Jan 1st, 2010, 12:04am
 
If you use loadBytes() You can eliminate spaces. Also, ENTER or just remember to ignore every 10th spot in the array. (and possibly 11th; see control character reference)

Code:
byte[] map = loadBytes("map.txt");
println(map); //Show contents of map

Using this as a map: Quote:
1111111111
1000000001
1011000101
1000000001
1000001001
1000000001
1001000001
1000001001
1000000001
1111111111

On my Windows 2K system, the above code printed:
[0] 49
[1] 49
...
[10] 13
[11] 10
[12] 49
[13] 48
...

Index "[10] 13" and "[11] 10" represent "Line Feed" and "Carriage Return" from ASCII.
Remember that the character '1' is ASCII(numeric value) 49.

References:
http://processing.org/reference/loadBytes_.html
http://processing.org/reference/char_.html

http://en.wikipedia.org/wiki/ASCII
http://en.wikipedia.org/wiki/Control_character
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #2 - Jan 1st, 2010, 1:24am
 
OK, this works good as long as I want to read the contents of a file digit by digit. But what if I wanted to read a file like that:

10 10
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 0 0 0 1 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 1 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1

where the two first numbers are the dimensions of the array following, so as to prepare the program to read it right?
Is there any way to do that? perhaps using Java commands?
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #3 - Jan 1st, 2010, 1:46am
 
Ah, then I would probably use loadStrings() and split() then convert each string to an int or char, etc.

It may actually be simpler even with the conversion.
Code:
String[] map = loadStrings("map.txt");
String[] dimensions = split(map[0], ' ');
println(dimensions); //shows [0] "10" and [1] "10"
...


http://processing.org/reference/loadStrings_.html
http://processing.org/reference/split_.html
http://processing.org/reference/int_.html
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #4 - Jan 1st, 2010, 3:13am
 
Well, this works fine, but only if the data are in one line...
I also found the splitTokens() command
http://processing.org/reference/splitTokens_.html
which gives the option to include various characters as split points. For example Space and Enter, in my case.
The problem is that it will not recognize (\n) or (^J) (linefeed) as Enter.
Could it have to do with the encoding of my .txt file
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #5 - Jan 1st, 2010, 3:41am
 
loadStrings() will remove the ENTER from each line so split should work.

If you do want to use splitTokens() with any String:
Are you using " \n" for the delimiter (both a space and new line switch)?
Or simply "\n" alone?

splitTokens() expects a String as a parameter.
Java should match "\n" to the file encoding of your system.
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #6 - Jan 1st, 2010, 3:51am
 
Well, here are the details of what's happening.
The tests have been made  with this txt file:
Quote:
1 1 0 0 1 1 0 0
1 1 0 0 1 1 0 0

1.This Code:

String[] map = loadStrings("map.txt");
String[] dimensions = split(map[0], ' ');
println(dimensions);

returns only the first 8 numbers
Quote:
[0] "1"
...
[7] "0"

so I thought I should use
2. This Code:

String[] map = loadStrings("map.txt");
String[] dimensions = splitTokens(map[0], "\n ");
println(dimensions);

but it returns exactly the same...

For you the split() command also works after the first line?
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #7 - Jan 1st, 2010, 4:10am
 
map[0] is just the first line of the file (created from loadStrings()).

"println(map.length);" will show you how many lines were in the file.

You can use a for loop to cycle through each line.
Code:
String[] dimensions = split(map[0], ' ');

//after map[0], we have what the map looks like
for (int i = 1; i < map.length; i++) {
 String[] rowData = split(map[i], ' ');
 //draw stuff
}
Re: reading numbers from txt file (HAPPY NEW YEAR!)
Reply #8 - Jan 1st, 2010, 4:27am
 
Yep, you are right... stupid me Cheesy
Thanks alot for the help!
I think this solves the issue for now...
Page Index Toggle Pages: 1