# Save and load string from text file

**URL:** <https://discourse.processing.org/t/save-and-load-string-from-text-file/7994>\
**Category:** Beginners\
**Created:** [January 31, 2019, 3:18pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994 "2019-01-31T15:18:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:18pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/1 "2019-01-31T15:18:29Z")

</div>

I’m trying to use loadString() and saveString() and convert the string to int and the other way around. But I get the errors

> "Type mismatch, “java.lang.String” does not match with “java.lang.String”

on `String loadHighScores = loadStrings(filename1);` and

> Type mismatch, “java.lang.String” does not match with “java.lang.String”

on `String saveHighScore[] = str(highScore);`

Here is the code:

```auto
  String loadHighScores = loadStrings(filename1);
  int highScore = int(loadHighScores);

  String saveHighScore[] = str(highScore);
  trim(saveHighScore);
  saveStrings(filename1, saveHighScore);

```

Also would this work with processing for Android?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:26pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/2 "2019-01-31T15:26:09Z")

</div>

see  
[https://processing.org/reference/loadStrings\_.html](https://processing.org/reference/loadStrings_.html)  
that as your error says, you need to think in array []

---

<div class="post-metadata">

**Author:** ![Quillbert1](https://avatars.discourse-cdn.com/v4/letter/q/f475e1/32.png) [@Quillbert1](https://discourse.processing.org/u/Quillbert1)\
**Post date:** [January 31, 2019, 3:27pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/3 "2019-01-31T15:27:13Z")

</div>

`LoadStrings()` and `SaveStrings()` use arrays of strings, not single strings.

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:29pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/4 "2019-01-31T15:29:29Z")

</div>

I added [] on

```auto
String[] loadHighScores = loadStrings(filename1);
int highScore[] = int(loadHighScores);

```

but then I get the same error on

```auto
str(highScore);

```

and I cant do

```auto
str(highScore[]);

```

How do I fix that?

EDIT: I can fix it with changing the normal int to int []but that wont work with my other code. Can I copy an int to an int[]?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:31pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/5 "2019-01-31T15:31:16Z")

</div>

pls use print to see what lines are in your file,  
and make loop over all lines.

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:33pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/6 "2019-01-31T15:33:24Z")

</div>

I get the error before I run the code. I added an edit “I can fix it with changing the normal int to int []but that wont work with my other code. Can I copy an int to an int[]?”

Would that work?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:34pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/7 "2019-01-31T15:34:56Z")

</div>

pls disable “bad” lines, using  
// disabled  
stay more close to the example from the reference

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:39pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/8 "2019-01-31T15:39:42Z")

</div>

I added `println(loadHighScores);` and it printed what’s in the file. Is that what you meant?

Edit again: Sorry I think I know what you meant. I printed the string and it said “[0]” then what was in the file

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:43pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/9 "2019-01-31T15:43:06Z")

</div>

and?? you find the line? with the number ?  
in the reference they do THIS:

```auto
println("there are " + loadHighScores.length + " lines");
for (int i = 0 ; i <loadHighScores.length; i++) {
  println(loadHighScores[i]);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:49pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/10 "2019-01-31T15:49:54Z")

</div>

int highScore = int(loadHighScores[0]);

might work then

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:50pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/11 "2019-01-31T15:50:04Z")

</div>

I got

```auto
there are 1 lines
28

```

So I added the lines

```auto
int i = 0;
  highScore = int(loadHighScores[i]);

```

the loadString now works. Thank you so much.

Is there anything similar I can do to `String saveHighScore[] = str(highScore);`?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 31, 2019, 3:53pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/12 "2019-01-31T15:53:15Z")

</div>

again see

> **[saveStrings() / Reference](https://processing.org/reference/saveStrings_.html)**
>
> Writes an array of Strings to a file, one line per String. By default, this file is saved to the sketch's folder. This folder is opened by selecting "Show Sketch Folder" from the "Sketch" menu. \<…

> [@Banansplitt](#):
>
> `String saveHighScore[] = str(highScore);`

is again string array string, can not work.

```auto
String[] saveHighScore;
saveHighScore[0]=str(highScore);

```

might work;

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 3:56pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/13 "2019-01-31T15:56:37Z")

</div>

The saving works, but how do I make an int into a String?  
[https://processing.org/reference/strconvert\_.html](https://processing.org/reference/strconvert_.html) only shows for "String"s and not for "String[]"s

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [January 31, 2019, 4:43pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/14 "2019-01-31T16:43:09Z")

</div>

> [@kll](#):
>
> String saveHighScore;

Sorry for late reply. Did some testing but I got a NullPointerException on `saveHighScore[0]=str(highScore);` when I change the value of highScore

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 1, 2019, 1:08pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/15 "2019-02-01T13:08:00Z")

</div>

try this, but it expects that the file with one line and one number in it already exists.

```auto
int myscore = 0;

String filename = "data/scores.txt"; // ! file must exists, have one line with one integer
String[] lines;

void setup() {
  size(200, 200);
  lines = loadStrings(filename);
  println("read scores from "+filename);
  println("got "+lines.length+" lines ");
  for ( int i =0; i<lines.length; i++) println("i "+i+" "+lines[i]);
  myscore = int(lines[0]);
  println("myscore "+myscore);
}

void draw() {
  myscore = int(random(7, 78));
}

void keyPressed() {
  if ( key == 's' ) {
    lines[0] = str(myscore);
    saveStrings(filename,lines);
    println("saved to "+filename);
    exit();
  }
}

```

---

<div class="post-metadata">

**Author:** ![Banansplitt](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Banansplitt](https://discourse.processing.org/u/Banansplitt)\
**Post date:** [February 2, 2019, 11:53am UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/16 "2019-02-02T11:53:29Z")

</div>

That worked! Thank you so, so much!!

---

<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:** [February 2, 2019, 7:23pm UTC](https://discourse.processing.org/t/save-and-load-string-from-text-file/7994/17 "2019-02-02T19:23:36Z")

</div>

A String is a Word or a text.

The [] always indicates that we have an Array of something, a list.

In your case a list of Strings (String[]). For a high score that means, first line in the list is `lines[0]`, 2nd is lines[1] etc., could be Paul 10 in 1st line, Frank 8 in 2nd line. To read all lines, often for-loops are used to loop over the lines in the list.

see

[https://processing.org/tutorials/arrays/](https://processing.org/tutorials/arrays/)
