Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:35:23+00:00 2026-06-18T09:35:23+00:00

In my program when the player submits a score it gets added to a

  • 0

In my program when the player submits a score it gets added to a local text file called localHighScores. This is list of the top five score the player has achieved while on that specific device.

I wasn’t sure how to write to a new line using FileOutputStream (if you know please share), so instead I’ve inputted a space in between each score. Therefore what I am trying to do is when the player clicks submit the program will open the file and read any current data is saved. It will save it to an String Array, each element being one of the five score in the text file and when it hits a ‘space’ in the fie it will add the score just read to the write array element

The code I currently have is as follows:

    String space = "  ";
    String currentScoreSaved;

    String[] score = new String[5]; 

    int i = 0;
    try
    {           
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput("localHighScore.txt")));
        String inputString;StringBuffer stringBuffer = new StringBuffer();

        while ((inputString = inputReader.readLine()) != null && i < 6)
        {
            if((inputString = inputReader.readLine()) != space)
            {
                stringBuffer.append(inputString + "\n");
                i++;
                score[i] = stringBuffer.toString();
            }
        }
        currentScoreSaved = stringBuffer.toString();
        FileOutputStream fos = openFileOutput("localHighScore.txt", Context.MODE_PRIVATE);

        while (i < 6)
        {
            i++;

            fos.write(score[i].getBytes());
            fos.write(space.getBytes());

        }       
        fos.write(localHighScore.getBytes());
        //fos.newLine(); //I thought this was how you did a new line but sadly I was mistaken

        fos.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();            
    }

Now you will notice this doesn’t re arrange the score if a new highscore is achieved. That I am planning on doing next. For the moment I am just trying to get the program to do the main thing which is read in the current data, stick it in an Array then print it back to that file along with the new score

Any Ideas how this might work, as currently it’s printing out nothing even when I had score in the textfile before hand

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:35:25+00:00Added an answer on June 18, 2026 at 9:35 am

    I’m only a first year student in Java programming and I am a new user here at stackoverflow.com, so pardon me if coding for android has some special rules I don’t know about, which prevents this simple and humble example from working. But here is how I would read from a file in the simplest of ways.

    File tempFile = new File("<SubdirectoryIfAny/name_of_file.txt");
    
    Scanner readFile = new Scanner( tempFile );
    
    // Assuming that you can structure the file as you please with fx each bit of info
    // on a new line.
    
    int counter = 0;
    while ( readFile.hasNextLine() ) {
        score[counter] = readFile.nextLine();
        counter++;
    }
    

    As for the writing back to the file? Put it in an entirely different method and simply make a simplified toString-like method, that prints out all the values the exact way you want them in the file, then create a “loadToFile” like method and use the to string method to print back into the file with a printstream, something like below.

    File tempFile = new File("<SubdirectoryIfAny/name_of_file.txt");
    
    PrintStream write = new PrintStream(tempFile);
    
    // specify code for your particular program so that the toString method gets the 
    // info from the string array or something like that.
    
    write.print( <objectName/this>.toStringLikeMethod() );
    // remember the /n /n in the toStringLikeMethod so it prints properly in the file.
    

    Again if this is something you already know, which is just not possible in this context please ignore me, but if not I hope it was useful. As for the exceptions, you can figure that you yourself. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program builds a dictionary out of a list based on what two index
This is output from my program sending agent update Created new player Identified sending
I want to make some GUI mockup program for video player, so my idea
I'm writing a tic tac toe program that plays throuh the terminal/console After Player
OK, here is a program that prompts a user for basketball player input. Well
Program that calculates and shows the value of (2 to the 10th power) This
Program runs once and it both throws data to the pipe and gets it
I want my program to work sort of like Team Player . Multi mice,
So I'm writing a program that plays Reversi/Othello against a player. I wrote a
I'm using the Windows Media Player OCX in a program runned on hundreds of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.