1 Replies - 5688 Views - Last Post: 07 April 2008 - 08:48 AM Rate Topic: -----

#1 whatevany  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 30-March 08

using the parse methods with arrays

Post icon  Posted 07 April 2008 - 08:28 AM

My program goes like this: "A Java program should be created to read a studen information text file (Grades.txt) and decide which student will not be required to take the final exam. List of students should be listed to screen and written to text file named (exempt.txt). During semester 4 tests are given and the following must be true for exemption from final exam:

*a student must have taken all 4 tests and have no test grade less than 80
*after the lowest of the four tests is dropped, the student must have an average 90 or better."

I have the code written but I still have a couple of problems:

1)When doing the if statement to decide which students are exempt, I get this error message:

exemptions.java:46: operator >= cannot be applied to double[],int
if (grades >=80 && average >=90)

2)How do I use the parse method with an array so I can read the grades from the text file? (It worked before I added the array)

Thanks again!

 import java.io.*;//needed for File and IO exception
import java.util.StringTokenizer;//to tokenize strings
import java.util.Scanner;//to use scanner class

public class exemptions
{
	public static void main(String[] args) throws IOException
	{
		String line, fileIn="Grades.txt", fileOut="Exempt.txt";//declares line, Grades and Exempt text files
		StringTokenizer tokens;//declares StringTokenizer variable tokens
		String SSnumber;//string variable SSnumber
		
		//build input stream	
		FileReader fr=new FileReader(fileIn);
		BufferedReader inFile=new BufferedReader(fr);
		
		//build output stream	
		FileWriter fw=new FileWriter (fileOut);
		BufferedWriter bw=new BufferedWriter(fw);
		PrintWriter outFile=new PrintWriter(bw);
		
			
		line=inFile.readLine();
		
		while (line!=null)
		{
			
			double []grades=new double[5];//create an array
			double lowest=grades[0];//store first variable in grades array in the variable lowest

			tokens=new StringTokenizer(line);
			SSnumber=null;
			SSnumber=tokens.nextToken();
			
			for(int i = 1; i <grades.length; i++)//for loop to get lowest grade 
			{
				if(grades[i]<lowest)
				lowest=grades[i];
			}
			
			double total=0;//accumulator
			double average;
			for (int i=0; i < grades.length; i++)//for loop to get average of grades
				total +=grades[i];
				average=(total-lowest)/grades.length;//average equals total minus lowest score divided by		 
																//number of grades
																
		if (grades >=80 && average >=90)//if grades are above 80 and average is above 90
																
		System.out.println(SSnumber +  " Average " + average);//print to screen the student's SS number & average  
		 																		//who are exempt from final
																				
		outFile.print(SSnumber);//print to text file "exempt.txt" 						
		outFile.print(average);//print to text file "exempt.txt"
		
		line=inFile.readLine();//read next line from file
	
		}
	
		inFile.close();//close input file "grades.txt"
		outFile.close();//close output file "exempt.txt"		
		}
}




Is This A Good Question/Topic? 0
  • +

Replies To: using the parse methods with arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: using the parse methods with arrays

Posted 07 April 2008 - 08:48 AM

View Postwhatevany, on 7 Apr, 2008 - 08:28 AM, said:

exemptions.java:46: operator >= cannot be applied to double[],int
if (grades >=80 && average >=90)


grades is an array of double

so you can do

if(grades[0] >= 80)
if(grades[1] >= 80)
if(grades[..] >= 80)
if(grades[n] >= 80)

but not

if(grades >= 80)


Why do I think that I have seen that code before ? :angry:

As far as your parsing is concerned

			for(int i = 1; i <grades.length; i++)//for loop to get lowest grade 
			{
				String str = tokens.nextToken();
				grades[i] = Double.parseDouble(str);
				if(grades[i]<lowest)
				lowest=grades[i];
			}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1