# Name in an Highscore

**URL:** <https://discourse.processing.org/t/name-in-an-highscore/18706>\
**Category:** Libraries\
**Created:** [March 16, 2020, 5:05pm UTC](https://discourse.processing.org/t/name-in-an-highscore/18706 "2020-03-16T17:05:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnGreen](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JohnGreen](https://discourse.processing.org/u/JohnGreen)\
**Post date:** [March 16, 2020, 5:05pm UTC](https://discourse.processing.org/t/name-in-an-highscore/18706/1 "2020-03-16T17:05:22Z")

</div>

Hi, I’m new to this forum, but I had to create a game using Processing and now I’m trying to add an Highscore, in which I’d like to put both name and score. I managed to add the score and I used ControlP5 to write the name, but I don’t really know how to pair name and score and how to create a sort of ‘name score’.  
I thought that maybe I could create a Class with the name and the score so as to pair them…but I’m not sure…Thank you !!

Here is my code: (I didn’t share HighScore.txt and ScoreNames but you can create them yourself :).  
If you can’t read it please tell me

```auto
int[] Record = new int[5];
int score, lines;
String [] RecordPoints, RecordNames; 
import controlP5.*; 
ControlP5 control;
Button Submit;
Textfield NameText;
String name;
int temp=0;
void setup() {
  size(600, 600);
  textSize(30);
  control= new ControlP5(this);
  RecordNames=loadStrings("ScoreNames.txt");
  RecordPoints=loadStrings("HighScore.txt");
  Record=int(RecordPoints);
  NameText=control.addTextfield("Name").setPosition(350, 460).setSize(100, 30).setAutoClear(false);//TextField classe di control credo, Name nome, setAutoClear per essere sicuri non si cancelli da solo
  Submit=control.addButton("Submit").setPosition(350, 500).setSize(100, 25); //bottone da cliccare 
  NameText.setColorForeground(color(#ABACB4)).setColorBackground(color(#71727E));
  Submit.setColorForeground(color(#ABACB4)).setColorActive(color(#AFB0D3)).setColorBackground(color(#71727E));
}

void draw() {
  background(0);
  for (lines=0; lines<RecordNames.length; lines++)
    text(RecordNames[lines], 250, 120+60*lines);
  for (int i=0; i<Record.length; i++) {
    text(Record[i], 50, 120+60*i);
  }
  if (Submit.isPressed()) {
    name=NameText.getText();
    temp=1;
  }
  if (temp==1)
    text(name, 500, 200);

  text(score, 180, 120);
}

void addNewScore(int score) {
  for (int i=0; i<Record.length; i++) {
    if (score>=Record[i]) {
      for (int j=Record.length-1; j>i; j--) {
        Record[j] = Record[j-1];
      }
      Record[i] = score;
      break;
    }
  }
}

void keyPressed() {
  score = int(random(1000));
  addNewScore(score);
  if (key==CODED) {
    if (keyCode==UP) {
      RecordPoints= str(Record);
      saveStrings(dataPath("HighScore.txt"), RecordPoints);
      saveStrings(dataPath("Scorenames.txt"), RecordPoints);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![The-Coder](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/the-coder/32/8127_2.png) [@The-Coder](https://discourse.processing.org/u/The-Coder)\
**Post date:** [March 16, 2020, 5:23pm UTC](https://discourse.processing.org/t/name-in-an-highscore/18706/2 "2020-03-16T17:23:47Z")

</div>

Can you please use the “format this like code” button so we can read your code.

---

<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:** [March 28, 2020, 3:37pm UTC](https://discourse.processing.org/t/name-in-an-highscore/18706/3 "2020-03-28T15:37:50Z")

</div>

If you need a class for mixed data types (like numbers and strings) that you plan to save to / load from a text file, use Table and TableRow.

[https://processing.org/reference/Table.html](https://processing.org/reference/Table.html)  
[https://processing.org/reference/TableRow.html](https://processing.org/reference/TableRow.html)

Then you can loadStable and saveTable rather than using saveStrings.

```auto
table = new Table();
  
table.addColumn("name", Table.STRING);
table.addColumn("score", Table.INT);
  
// ...

TableRow row = table.addRow();
row.setString("name", name);
row.setInt("score", score);

// ...

saveTable(table, "scores.csv");

```
