-2

The question I am trying to solve is from the book Building Java Programs and is formatted as follows:

Write a method called coinFlip that accepts a Scanner representing an input file of coin flips that are head (H) or tail (T). Consider each line to be a separate set of coin flips and output the number and percentage of heads in that line. If it is more than 50%, print "You Win!".

Consider the following file:
H T H H T
T t t T h H

For the input above, your method should produce the following output:

3 heads (60.0%)
You Win!
2 heads (33.3%)

When I run the code it only outputs: "0 heads (0.0)". So i'm assuming it never enters the second while loop for some reason or I am using the "next" methods wrong.

import java.io.*;
import java.util.*;
public class CoinFlip {
    public static void main(String[] args) throws FileNotFoundException{
          Scanner input = new Scanner("Series1.txt");
          PrintStream output = new PrintStream("Output.txt");
          coinFlip(input, output);
    }
    public static void coinFlip(Scanner input, PrintStream output) {
        while(input.hasNextLine()) {
            Scanner linesc = new Scanner(input.nextLine());
            int headCount = 0;
            int totalNums = 0;
            while(linesc.hasNext()) {
                String letter = linesc.next();
                if(letter.equalsIgnoreCase("H")) {
                   headCount++;
                }
            totalNums++;
            }

            double percent = findPercentage(headCount, totalNums);
            output.println(headCount + " heads " + "(" + percent +")");
            if(percent > 50.00) {
                output.println("You win!");
            }

        }
    }
    public static double findPercentage(int num1, int num2 ) {
       double percentage = (double)num1/num2 * 100;
       return percentage;
    }
}
sparkitny
  • 1,503
  • 4
  • 18
  • 23
  • You need to debug your program to figure out exactly what's going on. Here's one hint though: don't use `==` to compare `String` values. Use the `equals()` method instead. – Kevin Workman Jan 09 '18 at 00:20
  • I just changed it to letter.equalsIgnoreCase("H") but it stills outputs the same output – Pedro Gonzalez Jan 09 '18 at 00:28
  • Then please debug your program. Where exactly does the program's execution differ from what you expected? Don't assume it never enters the second while loop. Confirm whether that's true, and then investigate why. – Kevin Workman Jan 09 '18 at 00:29
  • As suggested, I tried to debug my code multiple times ( manually because I have yet to learn how to use the eclipse debugger) and I had a peer review my code. He believes there is nothing wrong with my code and that logically it should work out. There is, however, a warning stating: "Resource leak: 'linesc' is never closed ". Could that maybe be the problem? Thanks for your help, I really do appreciate it. – Pedro Gonzalez Jan 09 '18 at 02:16
  • There's plenty of resources elsewhere to help you use the Eclipse debugger – OneCricketeer Jan 09 '18 at 02:35

1 Answers1

1

1 - it is equalsIgnoreCase - you are missing a s

if (letter.equalsIgnoreCase("H")) {

2 - you are not reading from file Series1.txt, you are reading the String "Series1.txt". Use

Scanner input = new Scanner(new File("Series1.txt"));

Debugging, as already sugested, should have helped. A very easy alternative: add System.out.println(letter); before testing its value...

user85421
  • 28,957
  • 10
  • 64
  • 87
  • Ideally, the file should be read as a stream from the class loader – OneCricketeer Jan 09 '18 at 02:41
  • @cricket_007 maybe, depends where the file is located, is it a JAR, other conditions (like reading from standard input) – user85421 Jan 09 '18 at 02:43
  • 1
    @PedroGonzalez again, you should learn to use the debugger if you want to do serious programming... – user85421 Jan 09 '18 at 02:44
  • In my experience, just `new File` hasn't been super reliable, at least working between different IDEs – OneCricketeer Jan 09 '18 at 02:49
  • 1
    @cricket_007 20 years of Java, can;t say it is unreliable... more like the IDEs that use different default working directories (changeable by settings). `File` is just a path, well, in that case just the file name. – user85421 Jan 09 '18 at 02:53
  • I mean I use src/main/resources now, but before, the file was read outside the src directory, which isn't really intuitive when looking at the IDE workspace – OneCricketeer Jan 09 '18 at 02:58
  • @cricket_007 agreed (at least for eclipse), but I also would never use Scanner for this... actually I would not use a file at all, just *read* from a literal String (almost as already done in the question, just the input text) – user85421 Jan 09 '18 at 03:10