6 Replies - 8297 Views - Last Post: 23 October 2008 - 09:29 AM Rate Topic: -----

#1 MrHartgrave   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-October 08

GPA calculator

Post icon  Posted 21 October 2008 - 08:33 AM

I'm really banging my head against the wall with this.

My code has alot of problems, if you can help me with any of
them I would really appreciate it, thanks. :)

//program will ask how many grades(A B C D or F)
// it will display the GPA using the scale
// A=4, B=3, C=2, D=1 and F =0.
// It will also display a list of grades at the end that the user entered.
// deal with exeptions.
// use programmer defined method displayGPA.

(New code has been posted below)

This post has been edited by MrHartgrave: 21 October 2008 - 10:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: GPA calculator

#2 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: GPA calculator

Posted 21 October 2008 - 09:51 AM

Your first problem that I see is the for line in your main method.

It should be this...

for (int i = 1; i <= total; i++) // notice the first part

// you declared the wrong variable.


The second problem, is in the displayGPA(int, int, String) method, mainly with the System.out.println(file); line. That will print out the memory address the file is using. It will not print something you want (unless, of course, you want the memory address).

If you want to print out the variable you read in, you need to change it to this.

System.out.println(line); // 'line', not 'file'


Hope this helps! :D

EDIT: You also never use the points variable in your main method.

In your case statements, I think you mean totalGrade += THE NUMBER, not points, because that is always zero.

This post has been edited by Locke37: 21 October 2008 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 MrHartgrave   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-October 08

Re: GPA calculator

Posted 21 October 2008 - 10:15 AM

Hey thanks alot, love your avatar by the way! :)

I made the changes so now I have...

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

public class StudentGPA
{
	public static void main(String[] args)
	{

	int total;
	String grade;
	String filename;
	int totalGrade;
	int points;
	char letterGrade;

	points = 0;

		Scanner keyboard = new Scanner(System.in);

		System.out.println("Welcome to the GPA calculator!");
		System.out.print("How many grades will you enter?: ");
		total = keyboard.nextInt();

		System.out.println("Please name a file to save your grades in(end with .txt): ");
		filename = keyboard.nextLine();

		PrintWriter outputFile = new PrintWriter(filename);

		for (int i = 1; i <= total; i++)
	{
		System.out.println("Please enter a grade (A,B,C,D, or F) : ");
		grade = keyboard.nextLine();
		outputFile.println(grade);
		letterGrade = grade.charAt(0);

		switch(letterGrade)
		{
			case 'a':
			case 'A':
				totalGrade = points + 4;
				break;
			case 'b':
			case 'B':
				totalGrade = points + 3;
				break;
			case 'c':
			case 'C':
				totalGrade = points + 2;
				break;
			case 'd':
			case 'D':
				totalGrade = points + 1;
				break;
			case 'f':
			case 'F':
				totalGrade = points + 0;
				break;
			default:
				System.out.println("That was not a valid letter grade, try again.");

		outputFile.close();

		displayGPA(totalGrade,total,filename);


	}


	public static void displayGPA(int totalGrade,int total,String filename)

	{

		Find file = new File(filename);
		Scanner inputFile = new Scanner(file);

		String line = inputFile.nextLine();

		System.out.println("Your grades were : ");
		System.out.println(line);
		System.out.println("Your GPA is: " + totalGrade/total);

		inputFile.close();

		}
	}

  }
} 


I also get this error...

F:\Java Programs\StudentGPA.java:79: illegal start of expression
public static void displayGPA(int totalGrade,int total,String filename)
^
F:\Java Programs\StudentGPA.java:95: ';' expected
}
^
2 errors
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3601
  • View blog
  • Posts: 16,469
  • Joined: 20-September 08

Re: GPA calculator

Posted 21 October 2008 - 12:50 PM

It's generally OK but the following irons out the remaining problems:

import java.io.*;

// program will ask how many grades(A B C D or F)
// it will display the GPA using the scale
// A=4, B=3, C=2, D=1 and F =0.
// It will also display a list of grades at the end that the user entered.
// deal with exeptions.
// use programmer defined method displayGPA.
import java.util.Scanner;


public class StudentGPA {
	public static void main(String[] args) throws IOException {
		int total;
		String grade;
		String filename;
		int totalGrade;
		int points;
		char letterGrade;

		totalGrade = 0;
		points = 0;

		Scanner keyboard = new Scanner(System.in);

		System.out.println("Welcome to the GPA calculator!");
		System.out.print("How many grades will you enter?: ");
		total = Integer.parseInt(keyboard.nextLine());

		System.out.println(
			"Please name a file to save your grades in(end with .txt): ");
		filename = keyboard.nextLine();

		PrintWriter outputFile = new PrintWriter(filename);

		for (int i = 1; i <= total; i++) {
			System.out.println("Please enter a grade (A,B,C,D, or F) : ");
			grade = keyboard.nextLine();
			outputFile.println(grade);
			letterGrade = Character.toUpperCase(grade.charAt(0));

			switch (letterGrade) {
			case 'A':
				totalGrade += points + 4;

				break;

			case 'B':
				totalGrade += points + 3;

				break;

			case 'C':
				totalGrade += points + 2;

				break;

			case 'D':
				totalGrade += points + 1;

				break;

			case 'F':
				totalGrade += points + 0;

				break;

			default:
				System.out.println(
					"That was not a valid letter grade, try again.");
			}
		}

		outputFile.close();

		displayGPA(totalGrade, total, filename);
	}

	public static void displayGPA(int totalGrade, int total, String filename)
		throws IOException {
		File file = new File(filename);
		Scanner inputFile = new Scanner(file);

		String line = inputFile.nextLine();

		System.out.println("Your grades were : ");
		System.out.println(file);
		System.out.println("Your GPA is: " + (1.0 * totalGrade / total));

		inputFile.close();
	}
}



Was This Post Helpful? 1
  • +
  • -

#5 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: GPA calculator

Posted 21 October 2008 - 07:30 PM

I case you didn't notice the changes, you were missing a brace or two.

The avatar...you a FF fan? :)
Was This Post Helpful? 0
  • +
  • -

#6 MrHartgrave   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-October 08

Re: GPA calculator

Posted 23 October 2008 - 07:01 AM

Hey guys, thanks for all the help! :)

Yea I'm a fan of the old school FF, not so much past FF7 thou, not even sure if I liked 7...it was more of a compulsion...:P Always meant to play 8 but I kinda lost the time...now I'm addicted to Rock Band. :P
Was This Post Helpful? 0
  • +
  • -

#7 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: GPA calculator

Posted 23 October 2008 - 09:29 AM

Nah, that's alright, Rock Band's good.

But if you get a chance...the FFs only get better from then on, in my opinion. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1