Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a new name and score to an existing 2D array + I/O streams?
    primarykey
    data
    text
    <p>So I have a Sudoku game and I'm trying to implement a High Score system <strong>(Records the top 5 players)</strong> for it based on the times of the players. The lower the time, the higher the rank in the High Score is. I have thought on how to make it through streams. However, I do not know how to add a <strong>NEW</strong> high score to my 2D array (<code>nametime[5][2]</code>). I can add one name and score, but I can't add a new one when I play my game again.</p> <p>Variables:</p> <pre><code> private String[][] nametime = new String[5][2]; private String[] names = new String[5]; private String[] times = new String[5]; </code></pre> <p>Here's what I've done so far:</p> <pre><code> //assigns values to names[] and times[] private void addNamesAndTimes(String name, String score){ names[0] = name; times[0] = score; System.out.println("Player: " + name + " || " + "Time: " + score); } </code></pre> <p>I only assigned name and score to their first indices, <code>names[0]</code> and <code>times[0]</code> because this is where my problem is. I don't know how to keep adding names and scores to my arrays.</p> <pre><code> //merges names[] and times[] array into one 2D array nametime[][] private String[][] mergeArrays(String[] a, String[] b){ for(int i = 0; i &lt; nametime.length; i++){ nametime[i][0] = a[i]; nametime[i][1] = b[i]; } return nametime; } </code></pre> <p>I decided to merge my <code>names[]</code> and <code>times[]</code> arrays because I'm gonna do something with the <code>times[]</code> array later on. (Comparisons of scores)</p> <pre><code>public void createScoreFile(String name, String score){ addNamesAndTimes(name, score); File file = new File("Scores.txt"); try { if(!file.exists()){ System.out.println("Creating new file..."); file.createNewFile(); } FileOutputStream fo = new FileOutputStream(file, true); PrintStream ps = new PrintStream(fo); for(int i = 0; i &lt; nametime.length; i++){ for(int j = 0; j &lt; nametime[0].length; j++){ ps.print(mergeArrays(names, times)[i][j] + "\t"); } ps.println(); } ps.close(); } catch(Exception ex) { ex.printStackTrace(); } } </code></pre> <p>This method is for creating my a file that stores my <code>nametime[][]</code> array</p> <pre><code>//reads the contents of the text file and outputs it back to nametime[][] public String[][] loadScoreFile(){ File file = new File("Scores.txt"); try { Scanner scan = new Scanner(file); for(int i = 0; i &lt; nametime.length; i++){ for(int j = 0; j &lt; nametime[i].length; j++){ nametime[i][j] = scan.next(); } } System.out.println("Nametime Array:"); for(int i = 0; i &lt; nametime.length; i++){ for(int j = 0; j &lt; nametime[i].length; j++){ System.out.print(nametime[i][j] + "\t"); } System.out.println(); } } catch(Exception ex) { ex.printStackTrace(); } return nametime; } </code></pre> <p>This method is for reading my Score.txt file</p> <pre><code>//method for showing the table and frame public void showFrame(){ table = new JTable(loadScoreFile(), header); *other GUI stuff here* } </code></pre> <p>I would appreciate anyone who can help me!</p> <p>Edit: Here's to reiterate my problem once again. When I'm done playing the game, it should record the player name and score in the array and the .txt file. Then I close the game. When I open the game and finish playing again, it would have to store the name and score to the array and .txt file again. But in my case, that's not what's happening.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload