Loading...
Logo
Processing Forum

Text variables?

in Programming Questions  •  2 years ago  
hello, i was working on a simple battle simulator for an rpg. i know it's possible to use variables in text such as introducing a variable for score, then saying text(score,42,42). but is it possible to say something like "your current score is ____" and then put the variable into the blank, then keep writing after that? with the way i am doing it now, i will have "your current score is" and then in a seperate text (score), and then in another line, some other dialouge. is what i ask possible or just a fantasy?

Replies(2)

Re: Text variables?

2 years ago
text("your current score is "+score+" points");


Re: Text variables?

2 years ago
Java offers you lot of facilities to manipulate strings. Cedric shown the simple way, we mostly use, but there are some other ways, for example:
Copy code
  1. import java.util.*;
  2.  
  3. final String format = "This year is {0,number,integer} days " +
  4.     "{1,number,integer} hours {2,number,integer} minutes " +
  5.     "and {3,number,integer} seconds old";
  6.  
  7. void setup()
  8. {
  9.   Date date = new Date(); // Today
  10.   DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  11.   Date past = null;
  12.   try
  13.   {
  14.     past = new Date(df.parse("2011-01-01 00:00:00").getTime());
  15.   }
  16.   catch (ParseException e)
  17.   {
  18.     e.printStackTrace();
  19.     exit();
  20.     return;
  21.   }
  22.   long seconds = (date.getTime() - past.getTime()) / 1000;
  23.   long minutes = seconds / 60;
  24.   long hours = minutes / 60;
  25.   long days = hours / 24;
  26.   seconds -= minutes * 60;
  27.   minutes -= hours * 60;
  28.   hours -= days * 24;
  29.  
  30.   String message = MessageFormat.format(format,
  31.       days,
  32.       hours,
  33.       minutes,
  34.       seconds
  35.   );
  36.   println(message);
  37.  
  38.   String scoreMsg = "Your score is ] point%s";
  39.   String msg = String.format(scoreMsg, seconds, seconds == 1 ? "" : "s");
  40.   println(msg);
  41.   msg = String.format(scoreMsg, seconds * 314, seconds == 1 ? "" : "s");
  42.   println(msg);
  43.  
  44.   exit();
  45. }