-3

I'm having trouble displaying the score from my game in a text file, I have two text files, one for player details which works perfect but the other text file for scores is not appearing, after I enter the scores the time of the date modified in the scores text file is the same time I finish the game, its just the scores are not coming up. Here is my code:

import java.util.Scanner;
import java.io.*;

public class Project
{

    public static void main (String[]args) throws FileNotFoundException // This shows that the programme might encounter a file which is not there.
    {
        String outputFileName ="players.txt";
        //Storing the name of the text file

        PrintWriter writeToFile = new PrintWriter(outputFileName);
        //This allows me to write to the text file that I've called outputFileName


        Scanner playerStringInput = new Scanner(System.in);
        Scanner playerIntInput = new Scanner(System.in);
        //Taking info from the keyboard, one is for Integers and the other is for String

        String name = " ";
        String nickName = " ";
        String homeTown = " ";
        int age = 0;
        int previousGamesPlayed = 0;
        //Declaring the variables that I'm going to use to store the inputs from the programme

        int amtOfPlayers = 2; //The amount of players we have

        for(int i = 0; i < amtOfPlayers; i++) //Loop for both players
        {
            System.out.println("1.Please Enter Name");//Prints out instructitions for user.
            name = playerStringInput.nextLine();//Taking the input to store in name.
            writeToFile.println(name); //Outputs the data into the text file.

            System.out.println("2.Please Enter Nickname");//Printing out the nickname entered onto the screen.
            nickName = playerStringInput.nextLine(); //Taking the input to store in name.
            writeToFile.println(nickName); //Outputs the data into the text file.

            System.out.println("3.Please Enter Home Towm");//Printing out the hometown entered onto the screen.
            homeTown = playerStringInput.nextLine();//Taking the input to store in name.
            writeToFile.println(homeTown);//Outputs the data into the text file.

            System.out.println("4.Please Enter Age, this must be a number.");//Printing out the age entered onto the screen.
            age = playerIntInput.nextInt();//Taking the input to store in name.
            writeToFile.println(age);//Outputs the data into the text file.

            System.out.println("5.Please Enter Previous Games Played");//Printing out the previous games played entered onto the screen.
            previousGamesPlayed = playerIntInput.nextInt();//Taking the input to store in name.
            writeToFile.println(previousGamesPlayed);//Outputs the data into the text file.

            System.out.println("The name entered is: " + name); //Printing out the name entered onto the screen.
            System.out.println("The nickname entered is: " + nickName);//Printing out the nickname entered onto the screen.
            System.out.println("The hometown entered is: " + homeTown);//Printing out the hometown entered onto the screen.
            System.out.println("Age entered is: " + age);//Printing out the age entered onto the screen.
            System.out.println(previousGamesPlayed);//Printing out the name entered onto the screen.
        }

        writeToFile.close(); //Close the file writer so it can write the text into the file
        Scanner scan = new Scanner(System.in);//Allows us to take input from game

        String userInput1 = " ";
        char player1Input;

        String userInput2 = " ";
        char player2Input;
        //Declaring our variables for game

        for(int i = 0; i < 7; i++) //Loop for game which will loop 7 times
        {

        System.out.println("Player 1: Please Enter e.g R for Rock:");
        System.out.println("R.Rock");//Printing out Rock as choice for game
        System.out.println("P.Paper");//Printing out Paper as choice for game
        System.out.println("S.Scissors");//Printing out Scissors as choice for game

        userInput1 = scan.next();
        player1Input = userInput1.charAt(0);


        System.out.println("Player 2: Please Enter e.g S for Scissors");
        System.out.println("R.Rock");//Printing out Rock as choice for game
        System.out.println("P.Paper");//Printing out Paper as choice for game
        System.out.println("S.Scissors");//Printing out Scissors as choice for game



        userInput2 = scan.next();
        player2Input = userInput2.charAt(0);


        switch(player1Input)//beginning of switch statement
        {
            case 'R':  System.out.println ("Player 1:Rock");//

                switch(player2Input)
                {
                    //declaring possible outcomes if player 1 choses paper
                    case'R' : System.out.println("Player 2:Rock");//
                    System.out.println("The Game Result is Draw");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The Game Result is: Player 2 Wins");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println("The Game Result is: Player 1 Wins");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;//Terminating the loop before it goes onto the next statement

            case 'P' : System.out.println ("Player 1:Paper");
                switch(player2Input)
                {
                    //declaring possible outcomes if player 1 choses Paper
                    case'R' : System.out.println("Player 2:Rock");
                    System.out.println("The game result is: Player 1 Wins");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The game result is: Draw");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println("The Game Result is: Player 2 Wins2");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;//Terminating the loop before it goes onto the next statement

            //declaring possible outcomes if player 1 choses Scissors

            case 'S' : System.out.println ("Player 1:Scissors");
                switch(player2Input)
                {
                    case'R' : System.out.println("Player 2:Rock");
                    System.out.println("The Game Result is: Player 2 Wins");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The game result is: Player 1 Wins");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println ("The Game Result is: Draw");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;//Terminating the loop before it goes onto the next statement
        }//Ending of loop for game


        for(int h = 0; i < 2;i++);//for loop for scores, I also needed to call this int h as I already declared int I as a variable in my main method string.
        }

        int [] arrPlayer = new int[2];//using arrays to gather players score

        System.out.println("Player 1 Please Enter Your Score");//Player 1 printing out their score
        arrPlayer[0]=scan.nextInt();
        System.out.println("Player 2 Please Enter Your Score");//Player 2 printing out their score
        arrPlayer[1]=scan.nextInt();

        System.out.println("Player 1 : Score: " + arrPlayer[0]);//Player 1 score
        System.out.println("Player 2 : Score: " + arrPlayer[1]);//Player 2 score



        String outputFileName2 ="scores.txt";
        //Storing the name of the text file

        PrintWriter writeToFile2 = new PrintWriter(outputFileName2);
        //This allows me to write to the text file that I've called outputFileName

        writeToFile.close(); //Close the file writer so it can write the text into the file


    }//End of method
}//End of class
4
  • Try writeToFile2.flush() writeToFile2.close()...
    – brso05
    Dec 7, 2017 at 21:56
  • If you want to clear the question please consider deleting it instead of editing everything away, thanks.
    – Zabuzard
    Dec 7, 2017 at 22:17
  • Also, for the next time, try to create a minimal reproducible example. Your code contains much which is not relevant to the specific problem.
    – Zabuzard
    Dec 7, 2017 at 22:19
  • 1
    By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the CC BY-SA 3.0 license, for SE to distribute that content (i.e. regardless of your future choices). By SE policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. Please don't make more work for people by vandalizing your posts.
    – Makyen
    Dec 7, 2017 at 22:21

1 Answer 1

2

Your last statement should be writeToFile2.close(), not writeToFile.close(). You're closing the first file twice. The buffer is not being flushed to the file before the program ends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.