Save and load string from text file


#1

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:

  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?


#2

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


#3

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


#4

I added [] on

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

but then I get the same error on

str(highScore);

and I cant do

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[]?


#5

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


#6

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?


#7

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


#8

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


#9

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

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

#10

int highScore = int(loadHighScores[0]);

might work then


#11

I got

there are 1 lines
28

So I added the lines

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);?


#12

again see
https://processing.org/reference/saveStrings_.html

is again string array string, can not work.

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

might work;


#13

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


#14

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


#15

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

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



#16

That worked! Thank you so, so much!!


#17

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/