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

**URL:** <https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273>\
**Category:** Coding Questions\
**Created:** [June 24, 2019, 7:47pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273 "2019-06-24T19:47:54Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 24, 2019, 7:47pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/1 "2019-06-24T19:47:54Z")

</div>

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:

```auto
Bubblegum 654
Hello 765
loooool 235

```

But in the game leaderboard it’s showing like this:

```auto
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.

```auto
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");
  }
}

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [June 24, 2019, 8:00pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/2 "2019-06-24T20:00:45Z")

</div>

Hey There!

Trying a simple:

```auto
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 ?

---

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 24, 2019, 8:19pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/3 "2019-06-24T20:19:42Z")

</div>

Hello [InferNova](https://discourse.processing.org/u/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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 24, 2019, 8:26pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/4 "2019-06-24T20:26:07Z")

</div>

this seems to be problem

```auto
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:

```auto
Bubblegum,654
Hello,765
loooool,235

```

and then split each line at the “,” as shown here:

```auto
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

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [June 24, 2019, 8:29pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/5 "2019-06-24T20:29:06Z")

</div>

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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 24, 2019, 9:31pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/6 "2019-06-24T21:31:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [June 24, 2019, 9:32pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/7 "2019-06-24T21:32:27Z")

</div>

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

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [June 24, 2019, 9:34pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/8 "2019-06-24T21:34:42Z")

</div>

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.

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 24, 2019, 9:36pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/9 "2019-06-24T21:36:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 24, 2019, 9:56pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/10 "2019-06-24T21:56:25Z")

</div>

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 😛

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 24, 2019, 10:54pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/11 "2019-06-24T22:54:44Z")

</div>

```auto

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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![kartibrown](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kartibrown](https://discourse.processing.org/u/kartibrown)\
**Post date:** [June 25, 2019, 1:36am UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/12 "2019-06-25T01:36:26Z")

</div>

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

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

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

thanks again!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 25, 2019, 4:30am UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/13 "2019-06-25T04:30:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 2, 2019, 6:03pm UTC](https://discourse.processing.org/t/solved-strings-in-txt-file-wont-display-the-same-as-in-the-program/12273/14 "2019-07-02T18:03:59Z")

</div>

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

> [@Notes in Tables](https://discourse.processing.org/t/notes-in-tables/9712):
>
> I am looking for the best way to format a table that contains general notes on the data as well as the data itself. for example, the data could have typical column headers and each row would have corresponding entries. However, the notes could be text sentences, and could only occupy one row. I am trying to avoid a clumsy orgainization Thanks
