Hello! I’m new to programming in java Processing. I made my first game that actually works, but I can’t get the leaderboard to work. The strings in the .txt file looks like this:
Bubblegum 654
Hello 765
loooool 235
But in the game leaderboard it’s showing like this:
Bubblegum654
Hello765
loooool235
I’m not attaching the game code because it would be so much code. So, instead I’m attaching a small example.
import java.io.FileWriter;
import java.io.OutputStream;
FileWriter output;
OutputStream createFold;
File f = new File(dataPath("/LeadBoard.txt"));
/* what's in the file
LeadBoard.txt
Bubblegum 654
Hello 765
loooool 235
*/
void setup(){
size(640,360);
createFold = createOutput(dataPath("/dummy")); //creates the "data" folder
refreshFile("LeadBoard.txt");
}
void draw(){
background(200);
leaderBoard();
}
void leaderBoard() {
String[] a = loadStrings(dataPath("LeadBoard.txt"));
for (int i=0; i<a.length; i++) {
stringChanger(a[i]); //this is the function i have trouble with making.
}
stroke(0);
fill(255, 100, 100, 60);
rectMode(CENTER);
rect(width/2, height/2, 350, 200);
textAlign(LEFT);
fill(255);
int y=200;
for (int i=0; i<a.length; i++) {
text(a[i], width/2, height/2, 350, y);//displays the text in the LeadBoard.txt in the program. Like this:
y-=70; //Bubblegum654
} //Hello765
} //loooool235
String stringChanger(String stringToChange) {
int nON=0, nOC=0;
//nON=numberOfNumbers
//nOC=numberOfCharacters
for (int i=0, len=stringToChange.length(); i<len; i++) {
println("Counting Numbers and Character...");
if (Character.isDigit(stringToChange.charAt(i))) {
nON++; //counts the number of numbers it is in the string
println(nON);
}
if (Character.isLetter(stringToChange.charAt(i))) {
nOC++; //counts the number of Characters it is in the string
println(nOC);
}
}
println("Counting complete");
println("Changing string...");
String tempA; //creating a temporary String that holds the score
tempA=stringToChange.substring(nOC, stringToChange.length()); //tempA gets the score value. from the LeadBoard.txt| Hello(765)
stringToChange=stringToChange.substring(0, stringToChange.length()-nON); //removes the score. from: Hello765 to: Hello
stringToChange.concat("\t"+tempA); // adds a space and then the score. Like this: Hello 765
println("Change complete");
return stringToChange; //return the String that got changed
}
void refreshFile(String fileName) {
try {
output = new FileWriter(dataPath(fileName), true);
}
catch(IOException e) {
println("Directory for LeadBoard.txt does not exist");
println("Create a \"data\" folder in main directory");
}
}
println() just prints the text in the console bar, I want the text in a window in the program that’s why I’m using text()
but the text() outputs the text without the space and the println() outputs the text with spaces. That’s why I’m trying to add space between the username and the score.
Keep in mind that I’m new to programming so everything I say can be incorrect.
Thanks for the quick answer!
EDIT:
The game writes with “\t” space in the .txt file not the ordinary space
Maybe there is a tab or a wrong kind of space signs between each name and its score.
(Copy it into MS Word and switch on the display of extra signs)
better would be:
Bubblegum,654
Hello,765
loooool,235
and then split each line at the “,” as shown here:
String[]lines=
{
"sato,tanaka,yoshida,kida",
"saito,kawada,tanaka,sato",
"mizuno,hirayama,sato,kida",
"utsunomiya,sato,kawada",
};
String sato= "sato";
int count=0;
for (int i=0; i<lines.length; i++) {
String[]tokens=split(lines[i], ",");
for (int j=0; j<tokens.length; j++) {
if (tokens[j].equals(sato)) {
count++;
}
}
}
// ------------------------------------------------------
println(count);
produce your text file
You can produce your text file using saveStrings() on an Array of String. Each of its elements is formed as target[0] = name0+","+score0; or even in a for-loop.
I don’t want to produce a text file. I want to get the text in the .txt file to text() so it would render the text with the tab or “\t” spaces in the program.
If I use regular space then it works and renders with spaces but not with tab spaces. Should I do this with regular space instead?
The function textWithTab() takes care of the tab:
To do this we split (command split()) the entire line at the tabs and for loop over the components of the line. We text() out each component at the same y but with increasing x (add 211).
Since we increase x we make the tab distance / columns in the layout.