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 4 of 4

Thread: File input help

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default File input help

    So this is my first time on these forums so if this is in the wrong spot, I apologize.

    This is the input file I have:

    f   3.40
    f   4.00
    m   3.56
    m   3.80
    f   2.30
    f   3.95
    m   3.90
    m   4.00
    m   2.00
    f   4.00
    f   2.80
    m   3.70
    m   2.98
    f   3.89
    m   4.00
    f   3.90
    m   1.90
    m   2.90
    f   1.50
    f   2.67
    m   3.80
    m   2.35
    f   2.90
    f   3.70
    f   4.00
    m   3.78
    m   4.00
    f   3.98

    And here's my relevant code:

    public static void main(String[] args) throws FileNotFoundException
    	{
    			Scanner inFile = new Scanner (new FileReader("Ch5_Ex18Data.txt"));
    			PrintWriter outFile = new PrintWriter("output.txt");
    			int mCounter = 0, fCounter = 0;
    			double maleGPA, femaleGPA, mSum = 0, fSum = 0;
     
    			while (inFile.hasNext())
    			{
    				if (inFile.next() == "m")
    				{
    					mCounter++;
    					mSum = mSum + inFile.nextDouble();
    				}
    				else
    				{
    					fCounter++;
    					fSum = fSum + inFile.nextDouble();
    				}
    			}
    			inFile.close();

    What the program should do is store the values for the amount of males and females separately as well as the sum of their GPA's separately. Now with the way I have it everything is getting read in and stored as a female only. I've been trying to figure out what I have wrong for some time now and I can't get it, so I was hoping that someone here could point me in the right direction. If you need any more info or just need something cleared up just ask.

    Thanks in advance for any help you can provide.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: File input help

    So, how do you know that everything is being "stored as a female only"? I'm guessing that you added some other code at the end of main() that revealed this. It's a good idea to post that code as well: that way we have a specific problem to tackle. (Ie why is the output this ... rather than this ... that I was expecting?)

    ---

    Ok, so assuming you did add such code and as a result you really do know that the fCounter++ branch of the if statement is always being followed, then that must be because inFile.next() == "m" is always false. One thing to do is to add a line of code so that you can see exactly what inFile.next() is:

    while (inFile.hasNext())
    {
        String input = inFile.next();
        System.out.println("input was " + input);
        if (input == "m")
        {
            mCounter++;
            mSum = mSum + inFile.nextDouble();
        }
        else
        {
            fCounter++;
            f-Sum = fSum + inFile.nextDouble();
        }
    }

    The result shows that something odd is happening.

    In fact == is the wrong thing to use when comparing two things for equality. It's ok for so-called primitives like int etc. I mean if two ints are both 42 then they are equal! But if two words are both "foo" they might not be the same word: one might come at the start of a book and the other at the end. The two instances of the word "foo" would be equal, but not identical. The String class has an equals() method to test for this.

    //if (inFile.next() == "m")
    if (inFile.next().equals("m"))
    {

    Sorry for a long description to motivate a small change - but it's important to understand how to track down the bug to that one expression. And to see why it is wrong. The same thing applies to any non primitive (any class). Every class has an equals() method and it is used to define whatever the writer of that class intends "equals" to mean for instances of that class. In the case of strings equals() is implemented to return true if and only if the strings consist of the same letters in the same order, but this (although completely sensible) is somewhat arbitrary.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Freezer Boy (December 6th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: File input help

    Yeah sorry, I did have code at the end of main that printed out the total number of males and females as well as their average GPA.

    Anyway thanks for the help, that got it all to work right. I had forgotten that, like you said, there was a different way you have to compare strings , so I wasn't thinking of that.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: File input help

    You're welcome, I'm glad you've got it sorted out.

Similar Threads

  1. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  2. Input file to Array
    By Azayth in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2012, 03:05 AM
  3. how to get value path file from jsp form- input type file
    By meeGoreng in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: October 4th, 2011, 12:05 AM
  4. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM