input output java help

Started by
2 comments, last by spkenn5 15 years, 5 months ago
hi guys, i have this assignment to have a text file filled with data consisting of students, m and f and their scores next to it. (ex: f 3.8) and at the end, i need it to output the total #s of male and female, and the average GPAs for male and female. so far this is what i've got but it stopped me from compiling, saying that the while(gender != 'x') is an error. i hope you guys can help.

import java.io.*;
import java.util.*;

class Assignment6

{
	public static void main(String[] args)
						throws FileNotFoundException
	{
		 String str,
		 		gender;

		 double counter = 0;
		 double counter2 = 0;
		 double gpa = 0;
		 double sum1, sum2;
		 double average1, average2;

		 Scanner console = new Scanner(System.in);

		 Scanner inFile = new Scanner(new FileReader("gpa.txt"));
		 PrintWriter outFile = new PrintWriter("gpa.out");

		 gender = inFile.nextLine();
		 gpa = inFile.nextDouble();

		 while(gender != 'x')
		 {
			 if(gender == 'm')
			 {
				sum1+= gpa;
			    counter = counter + 1.0;
			    average1 = sum1/counter;
		     }
			else if(gender == 'f')
			{
				sum2+= gpa;
				counter2 = counter2 + 1.0;
				average2 = sum2/counter2;
			}
		}

		 System.out.println("The count is " + counter);
		 System.out.println("The average is = " + average1);
		 System.out.println("The count is " + counter2);
		 System.out.println("The average is = " + average2);

		outFile.print("There are " + counter + " male students.");
	    outFile.printf("Average GPA for male students is: " + average1);
	    outFile.print("There are " + counter2 + " female students.");
	    outFile.printf("Average GPA for female students is: " + average2);

	    outFile.close();
	 }
 }
and this is the textfile.

m 2.4
f 3.2
m 3.7
m 4.0
f 2.9
f 1.8
f 3.8
m 1.7
m 3.4
f 2.6
m 2.8
m 1.7
f 3.7
m 3.9
f 4.0
f 3.6
m 2.3
f 2.9
m 3.0
m 2.7
x
Thanks
Advertisement
Quote:Original post by spkenn5
... it stopped me from compiling, saying that the while(gender != 'x') is an error.

The error message that the compiler gives you should be quite clear. If there's something about the message that you don't understand feel free to ask.
This space for rent.
In Java, the "==" operator compares if two object references point to the same object. This is one of the weird things you have to get used to in Java. Every variable (with the exception of primitive data types) stores a memory address to an object that you have created. This means that you can have two variables pointing to the same memory address, or in other words, the exact same object. That is what the "==" operator does in Java. It checks if two variables store the exact same reference. Since 'x' is a char, which is a primitive data type, you can't compare it to another object with the "==" operator, because it can't point to any memory address, unlike object references. You should be using the String.equals(anotherString) method. The API documentation for the String class can be found here. Here is an example of how to compare strings in Java.

[edit]

Actually, it looks like you want to compare the first character in the gender string. You can get the first character by doing String.charAt(0).

Example:

while (gender.charAt(0) != 'x')
{
}

Here is some documentation on the charAt method.

[edit edit]

Looking at your code, I don't think it will work the way you want it to. If the gender and score are stored on the same line, then gender = inFile.nextLine(); will read the whole line, including the score, and put it into gender. That means that gpa = inFile.nextDouble(); would throw an exception because inFile would be on the next line, and the line starts with a character, not a double. What you should do instead is gender = inFile.next();, which will read the current line and store the text in gender until it comes across white space (a space, new line, or tab). Then gpa = inFile.nextDouble(); will read the next double, which will be the score right after the gender in the text file.

Note that if the this is only a problem if the file is stored like:

f 3.8
m 3.7

If the file is stored like this:

f
3.8
m
3.7

Then you don't have to worry about what I just said.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
@gumpy:
i don't know which equal and not equal form to use for this one.

@mike
do you know any web about it? because i dont know how to use it

This topic is closed to new replies.

Advertisement