#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    HW-how to read a string from a text file


    I received this homework assignment, it basically tells me to read GPAs of male and female students from a text file and then output averages to another text file.

    This is an idea of what the file looks like that is read:
    f 3.40
    f 4.00
    m 3.56
    m 3.80
    f 2.30
    f 3.95

    My question is how do I read whether the character before the gpa is "m" or "f"?

    This is what I have so far:

    Code:
    import java.io.*;
    import java.util.*;
    
    public class HW2 {
      
      public static void main (String[] args) throws FileNotFoundException {
        
        double mGPA, fGPA;
        String m,f;
        double mGPAsum = 0;
        double fGPAsum = 0;
        int numberMales = 0;
        int numberFemales = 0;
        
        
        Scanner inFile=new Scanner(new FileReader("input.txt"));
        PrintWriter outFile = new PrintWriter("outputGPA.txt");
        
        while (inFile.hasNext()) {
          
          if (inFile.equals("f")) {
            double gpa = inFile.nextDouble();
            fGPAsum += gpa;
            numberFemales++;
          }
          
          if (inFile.equals("m")) {
            double gpa = inFile.nextDouble();
            mGPAsum += gpa;
            numberMales++;
          }
        }
        
        outFile.println("Sum female GPA = " + fGPAsum);
        outFile.println("Sum male GPA = " + mGPAsum);
        outFile.println("Female count = " + numberFemales);
        outFile.println("Male count = " + numberMales);
        
        fGPA = fGPAsum / numberFemales;
        mGPA = mGPAsum / numberMales;
        
        outFile.println("Average female GPA = " + fGPA);
        outFile.println("Average male GPA = " + mGPA);
        
        inFile.close();
        outFile.close();
      }
    }

    And this is what it's saying:

    2 warnings found:

    Warning: The local variable m is never read

    Warning: The local variable f is never read
  2. #2
  3. No Profile Picture
    Choose Life...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Location
    Scotland
    Posts
    461
    Rep Power
    513
    Well the warnings relate to the fact you have defined two variables called m and f, yet you dont initialise them or actually use them anywhere...
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    The Scanner class has methods that read data from the file and store it in a variable in the program. Read the AI doc:http://docs.oracle.com/javase/7/docs/api/

    Code:
    if (inFile.equals("m")) {
    Comparing the reference to the Scanner object: infile to a String doesn't make any sense.

    Also posted at http://www.javaprogrammingforums.com...text-file.html
    Last edited by NormR; October 1st, 2012 at 09:35 AM.

IMN logo majestic logo threadwatch logo seochat tools logo