This site requires JavaScript, please enable it in your browser!
Greenfoot back
mitrolex
mitrolex wrote ...

2017/3/8

Write Multiple Things To A File

1
2
3
4
mitrolex mitrolex

2017/3/8

#
How can i write a string to a file so that the last string that was in the file stays there and the new string goes to a new line?
Super_Hippo Super_Hippo

2017/3/8

#
In the other discussion, you had the code for writing to and reading from a file. I guess you could read what is in, save that as a String and add what you want to add to that String. You can start a new line with \n.
mitrolex mitrolex

2017/3/8

#
Thanks man. I managed to do it but can't get to make the new string to go to a new line. Tried the \n and f.newLine(); but none worked. :/ And how could i sort the content of the file by score?
Super_Hippo Super_Hippo

2017/3/8

#
Just for anyone in the future who finds this discussion and can't find / don't want to search for the other discussion: This was the other discussion I have never worked with writing to and from files, so maybe you have to wait for danpost to help you out on this one. However, show the code you have tried to get a new line. For the sorting, you would need to read out every line, convert it to an int and then compare and sort it. Like I already suggested in the other discussion, the UserInfo class is much easier to understand to handle than doing it manually. Did you already look into it if it would be working for you? Link to the UserInfo API
danpost danpost

2017/3/8

#
mitrolex wrote...
Thanks man. I managed to do it but can't get to make the new string to go to a new line. Tried the \n and f.newLine(); but none worked. :/ And how could i sort the content of the file by score?
The code given in the other thread for reading was for a one-line file. It needs some adjusting to make it work for multiple lines. I do not have the time now to show what needs done. Maybe someone else can do that.
mitrolex mitrolex

2017/3/8

#
I don't know if it would work for me and im pretty sure i'd have to change a lot of my code and i don't want that. I'm waiting for danposts or someone elses time now.. :-D
Nosson1459 Nosson1459

2017/3/9

#
String line;
int[] score = new int[/* amount of scores in file */]; // all the code here is good besides for this line (I think)
BufferedReader br;
try
{
    int loopCount = 0;
    br = new BufferedReader(new FileReader("score.txt"));
    while((line = br.readLine()) != null)
    {
        score[loopCount] = Integer.valueOf(line);
        loopCount++;
    }
    br.close();
}
catch (IOException ex)
{
    System.out.println(ex.getMessage());
}
danpost danpost

2017/3/9

#
The code provided by Nosson1459 will work as long as you absolutely know the number of scores that will be in the file and each line in the file contains exactly one score and nothing else.
mitrolex mitrolex

2017/3/9

#
Thanks Nosson but if danpost is right then that's not what i'm looking for. I don't wanna know the nubmer of scores and the file has to contain a name of the player and his score.
danpost danpost

2017/3/9

#
Best thing to do is create a format that will help in having the file read in properly. You could create one single string to read in and use a separator between fields and records. For example, a string could be:
"Billy Bob:2550::Bobby Sue:2280::"
Any number of names with scores can be concatenated to the string. Then, you can read the one line in with the original reading code from the other discussion thread and break it down after closing the file:
// count the records
String temp = line;
int recordCount = 0;
while (!"".equals(temp))
{
    temp = temp.substring(temp.indexOf("::")+2);
    recordCount++;
}
// parsing the data
Object[][] data = new Object[recordCount][2];
for (int i=0; i<recordCount; i++)
{
    int recLen = line.indexOf("::");
    String record = line.substring(0, recLen);
    line = line.substring(recLen+2);
    int nameLen = record.indexOf(":");
    data[i][0] = record.substring(0, nameLen);
    data[i][1] = Integer.valueOf(record.substring(namLen+1));
}
Then, for any record, 'n', the data is:
String name = (String)data[n][0];
int score = (Integer)data[n][1];
mitrolex mitrolex

2017/3/9

#
So what you are saying is that when i write a string to the file it neds to be
PlayerName:5::
? And after the code for reading i'd have to paste your code?
danpost danpost

2017/3/9

#
mitrolex wrote...
So what you are saying is that when i write a string to the file it neds to be
PlayerName:5::
? And after the code for reading i'd have to paste your code?
I was suggesting one possible way -- and yes, with that way, that is what you would do. The string needs to be similar to what you gave here, or longer, if you are saving multiple scores by multiple players (like I gave above). You would save exactly one string (just like you are reading back only one string).
mitrolex mitrolex

2017/3/26

#
Thank you danpost, but i have two more questions. In
String name = (String)data[n][0];
int score = (Integer)data[n][1];
what's 'n'? When i delete those two lines it gives me a "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" error at line 173 where
String record = line.substring(0, recLen);
is. What's wrong with that?
danpost danpost

2017/3/26

#
mitrolex wrote...
In
String name = (String)data[n][0];
int score = (Integer)data[n][1];
what's 'n'?
The 'n' is the record number, or the index for a specific "player/score" set of data within the 2D array. Its valid range of values is from zero to 'data.length-1'.
When i delete those two lines it gives me a "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" error at line 173 where
String record = line.substring(0, recLen);
is. What's with that?
Those last two lines (given above in this post) are to be used within the same method after the data is parsed (after line 19 above). The specific 'n' value will need to be determined by the match of the name of the player, if it exists.
mitrolex mitrolex

2017/3/26

#
I wanted to ask where and how should i define the 'n' variable because of "cannot find symbol - variable n"? I'm an idiot, i know.
There are more replies on the next page.
1
2
3
4