Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: HW-how to read a string from a text file

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HW-how to read a string from a text file

    I'm pretty confused by your approach here. The inFile variable is a Scanner, right? So why are you comparing it to a String? That's not how you test the Scanner's input. I suggest reading the API as well as the tutorial to figure out how to correctly use Scanner. By the way, those links were the first 2 hits for googling "java scanner", which is a process you should try to get more accustomed to.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: HW-how to read a string from a text file

    Also posted a HW-how to read a string from a text file - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is the best way to read from a text file?
    By Kerr in forum File I/O & Other I/O Streams
    Replies: 20
    Last Post: January 4th, 2012, 05:41 PM
  2. Read text file
    By cardamis in forum Java IDEs
    Replies: 2
    Last Post: November 4th, 2011, 11:59 PM
  3. Re: How to read a text from an Image file?
    By aparnaverma in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 5th, 2011, 08:01 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM