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.
Page Index Toggle Pages: 1
loading bytes (Read 467 times)
loading bytes
Jun 25th, 2006, 5:29pm
 
I am creating a simple game. i save the highscores in a file, highscore.txt, but then i want to pront them.

i use this code:
     byte s = loadBytes("highscore.txt");
     int q = s;
     text("High Score", 40, 400);
     text(q, 150, 400);
and, on the first line, it returns the error "type of the right sub-expression, "byte[]", is not assignable to th ariable of type "byte".

note:

i use the line :
     saveBytes("highscore.txt", score);
where score is the player's score, to save the high scores.
Re: loading bytes
Reply #1 - Jun 25th, 2006, 6:09pm
 
when loading bytes, you need to assign it to a byte-array, but i'm guessing you're looking for saveStrings() and loadStrings().

so something like:
loading:
String[] s = loadStrings("highscore.txt");

saving:
saveStrings("highscore.txt");
Re: loading bytes
Reply #2 - Jun 25th, 2006, 6:55pm
 
thanks, but now, when I try to print the strng, using:

text(s, 150, 400);

it returns: "perhaps you wanted the overloaded version "void text $1, float $2, float $3" istead?"

also, can i use the clause if (score > s) if i have s as a string loaded from the file? or is there a command to extract a numeric value from a string?

thanks.
Re: loading bytes
Reply #3 - Jun 25th, 2006, 7:25pm
 
read up on arrays in java..
but to answer your question use
String s = join(loadStrings("highscore.txt"),"\n"); // the \n is for linebreaks

now you've loaded the entire file as a long string, with linebreaks.

and numeric values from string, is int number = Integer.parseInt("123"); ..
Re: loading bytes
Reply #4 - Jun 26th, 2006, 3:16am
 
thank- hopefully just two more questions- if i want to save the integer named score to highscore.txt so it can be read by the line you suggested, how would I do so? also, when i run the line
      String s = join(loadStrings("gamedata.dat"), "\n" ); // the \n is for linebreaks

it returns a NullPointerExeption, although i have a document called gamedata.dat in the same folder as the program.
Page Index Toggle Pages: 1