3

I have a problem where I am asked to read the tokens from a file and print out three values: the number of tokens that are numbers ( doubles), the number of tokens that are not numbers, and the sum of the numbers. and just those values.

I've managed to read the text file, and have been able to separate them based on them being doubles or not, but I am having trouble structuring my output which at the moment is just the list as is except that the non-doubles are listed as such where my text file is:

    1 2 3 one two three
    z 1.1 zz 2.2 zzz 3.3 zzzz

And my code looks like:

     import java.util.Scanner;
     import java.io.File;
     import java.io.FileNotFoundException;


     public class Lab1 {

        public static void main(String[] args) {

     Scanner in = null;

     try {
         in = new Scanner(new File("data.txt"));
     } catch (FileNotFoundException e) {
         System.err.println("failed to open data.txt");
         System.exit(1);
     }

    /**
     * parse file token by token
     */

     while (in.hasNext()) {
         String token = in.next();
    // if it's a Double
         try {
             double d = Double.parseDouble(token);
             System.out.println(+d);
             continue;
     } 
         catch (NumberFormatException e) {

         // It's not a double:

             System.out.println("Not a double");

     }
   }
}

    }

And this is my output:

    1.0
    2.0
    3.0
    Not a double
    Not a double
    Not a double
    Not a double
    1.1
    Not a double
    2.2
    Not a double
    3.3
    Not a double

When I want my output to be this:

    6 7 12.6

Which corresponds to the number of doubles, number of non-doubles, and sum of doubles respectively.

If I am phrasing this wrong please forgive. Just looking to fix my output.

Thanks in advance!

RasecCesar
  • 33
  • 5
  • Could you clarify what you're stuck on that keeps you from getting your desired output? It looks like you need to use 3 variables for keeping track of the 2 counts and the running sum. – 4castle Aug 29 '17 at 00:08
  • Honestly I'm just kind of confused about what to do after starting to parse the file. I thought about making int for number of doubles, int for number of non-doubles, double for sum of doubles. I don't know how to structure that idea. – RasecCesar Aug 29 '17 at 00:11

2 Answers2

1

You need to

  1. Define variables at the top of your main function to remember the current counts of doubles, non-doubles, and the total sum of doubles.

    int doubles = 0;
    int nonDoubles = 0;
    double sumOfDoubles = 0.0;
    
  2. Add to the double/non-double/sum each time you read from the file

    while (in.hasNext()) {
        String token = in.next();
        try {
            // try to convert string to double
            double d = Double.parseDouble(token);
            doubles += 1;
            sumOfDoubles += d;
        } catch (NumberFormatException e) {
            // conversion failed - it's not a double
            nonDoubles += 1;
        }
    }
    
  3. Print the total counts at the end of the main function. Note that %.1f truncates to one decimal place.

    System.out.printf("%d %d %.1f", doubles, nonDoubles, sumOfDoubles);
    
the_storyteller
  • 2,335
  • 1
  • 26
  • 37
1

Since you have already managed to detect when the input is a double and when it is not a double, you can simply increase the counters respectively when each case is met, and show the output after you have finished reading the input. For example:

 import java.util.Scanner;
 import java.io.File;
 import java.io.FileNotFoundException;


 public class Lab1 {

    public static void main(String[] args) {

      Scanner in = null;

      try {
        in = new Scanner(new File("data.txt"));
      } catch (FileNotFoundException e) {
        System.err.println("failed to open data.txt");
        System.exit(1);
      }

   /**
    * parse file token by token
    */

     // Added lines of code
     int noOfDoubles = 0;
     int noOfNonDoubles = 0;

      while (in.hasNext()) {
        String token = in.next();
        // if it's a Double
        try {
          double d = Double.parseDouble(token);
          System.out.println(+d);
          noOfDoubles++;
          continue;
        } 
         catch (NumberFormatException e) {

         // It's not a double:

           System.out.println("Not a double");
           noOfNonDoubles++;

         }
      }

     System.out.println("No of doubles: " + noOfDoubles + ", " +
         "No of non-doubles: " + noOfNonDoubles);
   }

}

As for calculating the sum of the doubles present, you can either do as Bruce suggested in his answer, which is to add another counter in a similar fashion to the counters above for the sum. However, you might want to consider using BigDecimal instead, because of this issue: Java: Adding and subtracting doubles are giving strange results.

umop apisdn
  • 653
  • 9
  • 20