Jon Smith Jon Smith - 3 months ago 6
Java Question

How to have two outputs as an input for a function in Java

I'm creating a program in

Java
that reads a text file and displays the various outputs such as
total played
and
total won
.

Two out of the three outputs I have to display work perfectly which are the
total played
and
total won
but I can't get the last one to work, which is
return players
which takes
total played
and
total won
to produce the output.

What I have is:

List<Double> RP = data.RegexCheck(fileData.toString(),
"(Return Players: £) (totalPlayed/totalWon)*100",2);

//ReturnPlayers
double ReturnPlayers = 0;
for(double d : RP){
ReturnPlayers+= d;
}


I got given the fact that
return players
is
(totalPlayed/totalWon)*100
. How would I go about doing this?

Edit

The way that I read the text file in was by

try(BufferedReader br = new BufferedReader(new FileReader("myFile.txt"))){}


And the way that the outputs were written were by

System.out.println("The total times played is: " + totalPlayed);
System.out.println("The total times won is: " + totalWon);
System.out.println("Return Players value is: " + ReturnPlayers);


Edit #2

It is working, but the final output
RP
isn't displaying anything, only
0.0
, and I have a feeling that it should display a number as it takes in two outputs that do display numbers,
1240
and
320
respectively, and then multiplied by 100, which should give the display of
387.5


Edit #3

An example of
myFile.txt
is

Start Game
Game number: 1
Player: lose
End Game
Start Game
Game number: 2
Player: lose
End Game
Start Game
Game number: 3
Player: lose
End Game
Start Game
Game number: 4
Player: won
End Game
Start Game
Game number: 5
Player: won
End Game
Start Game
Game number: 6
Player: lose
End Game
Start Game
Game number: 7
Player: won
End Game
Start Game
Game number: 8
Player: won
End Game

Answer

Java code

try(BufferedReader br = new BufferedReader(new FileReader("myFile.txt"))){
    String line = null;
    int wonCounter = 0;
    int lostCounter = 0;

    while((line=br.readLine()) != null){
        if(line.contains("Player")){
            if(line.contains("won")){
                wonCounter++;
            }else{
                lostCounter++;
            }
        }
    }

    System.out.println("The total times played is: " + (wonCounter+lostCounter));
    System.out.println("The total times won is: " + wonCounter);
    System.out.println("Return Players value is: " + (wonCounter+lostCounter)*100.0/wonCounter);

} catch (IOException e) {
    e.printStackTrace();
}

Output:

The total times played is: 8
The total times won is: 4
Return Players value is: 200.0