[SOLVED] Strings in .txt file won't display the same as in the program

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");
  }
}
1 Like

Hey There!

Trying a simple:

String a[];
void setup() {
  size(600, 600);
  a = loadStrings("Example");
}
void draw() {
  for (int i = 0; i < a.length; i++) println(a[i]);
}

Produces desirable results. loadStrings() can read everything from the file, I am kind of confused why are you doing all the extra bits ?

Hello InferNova!

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

this seems to be problem

Bubblegum    654
Hello        765
loooool      235

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.

Chrisir

To print out in text the following can be done I used println() to just showcase

String a[];
void setup() {
  size(600, 600);
  a = loadStrings("Example");
}
void draw() {
  background(0);
  for (int i = 0; i < a.length; i++) text(a[i], width/2, 40 * i);
}

Yes I understand but if you use text() the text() seems to not output the "\t" or tabs spaces as you called them.

1 Like

I see the issue OP is potraying. Text really does not output out the tabs.

String a[];
void setup() {
  size(600, 600);
  a = loadStrings("Example");
  preLeaderBoard();
}
void draw() {
  background(0);
  for (int i = 0; i < a.length; i ++) text(a[i], width/2, 40*(i+1));
}

void preLeaderBoard() {
  for (int i = 0; i < a.length; i++) {
    a[i] = a[i].substring(0, a[i].indexOf(' '))+"\t"+a[i].substring(a[i].indexOf(' ')+1);
  }
}

Seems like a bug if you ask me but this quick of doing an actual tab and having it as a String does a quick trick for the fix.

String a[];
void setup() {
  size(600, 600);
  a = loadStrings("Example");
  preLeaderBoard();
}
void draw() {
  background(0);
  for (int i = 0; i < a.length; i ++) text(a[i], width/2, 40*(i+1));
}

void preLeaderBoard() {
  for (int i = 0; i < a.length; i++) {
    a[i] = a[i].substring(0, a[i].indexOf(' '))+"  "+a[i].substring(a[i].indexOf(' ')+1);
  }
}

Hi!

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?

1 Like

I think I’m too bad at programming because I don’t understand what your code does and when I ran it it gave me an

StringIndexOutOfBoundsException: String index out of range: -1

error so I guess I’ll have to stick to the regular spaces :stuck_out_tongue:

Thanks anyway for the quick answer and help. Really greatful!




String a[] = {
  "jkshs"+"\t"+"22", 
  "uzee"+"\t"+"33", 
  "popi"+"\t"+"334"
};
void setup() {
  size(600, 600);
}

void draw() {
  background(0);
  for (int i = 0; i < a.length; i ++) { 
    textWithTab(a[i], width/2-111, 40*(i+1));
  }
  stroke(255);
  line(width/2-111+200, 0, 
    width/2-111+200, 200);
}

void textWithTab(String s, float x, float y) {

  String[] sArray = s.split("\t");

  for (int i = 0; i < sArray.length; i++) {
    text(sArray[i], x, y);
    x+=211;
  }
}
2 Likes

You’re a miracle! This works! Thanks alot.

I have no clue how this works but that’s my problem now :smiley:

Now I’m of training what this code actually does haha

thanks again!

1 Like

It’s simple

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.

1 Like

You might also be interested in this past discussion of tabular data display and formatting: