# GPA calculator

Page 1 of 1

## 6 Replies - 8306 Views - Last Post: 23 October 2008 - 09:29 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=68422&amp;s=0a1e5cbd71ba77972b1d772d4e99b52c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MrHartgrave

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

# GPA calculator

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.

// 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

• Sarcasm Extraordinaire!

Reputation: 550
• 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!

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

### #3 MrHartgrave

Reputation: 0
• 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 filename;
int points;

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();

filename = keyboard.nextLine();

PrintWriter outputFile = new PrintWriter(filename);

for (int i = 1; i <= total; i++)
{

{
case 'a':
case 'A':
break;
case 'b':
case 'B':
break;
case 'c':
case 'C':
break;
case 'd':
case 'D':
break;
case 'f':
case 'F':
break;
default:
System.out.println("That was not a valid letter grade, try again.");

outputFile.close();

}

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(line);

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

### #4 g00se

• D.I.C Lover

Reputation: 3609
• Posts: 16,527
• 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 filename;
int points;

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(
filename = keyboard.nextLine();

PrintWriter outputFile = new PrintWriter(filename);

for (int i = 1; i <= total; i++) {

case 'A':

break;

case 'B':

break;

case 'C':

break;

case 'D':

break;

case 'F':

break;

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

outputFile.close();

}

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(file);

inputFile.close();
}
}

```

### #5 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• 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?

### #6 MrHartgrave

Reputation: 0
• 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... Always meant to play 8 but I kinda lost the time...now I'm addicted to Rock Band.

### #7 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }