Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with GPA calculator program

 
Darrien Kamai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello first time poster here! I am writing a program for my college class in which we are supposed to calculate GPA from an input file. These are the exact directions:
The course points equal (gradePoints x credits). Write a program that reads data from a text file that contains a sequence of (letter grade, credits) pairs, and calculates the overall GPA of the student. An example of the input may look something like this:
B- 3
D+ 2
A 3
W 0
C+ 4
B 3
Requirements:
1. Use a method that takes a letter grade and a number of credits as parameters, calculates and returns the points for the course.
2. All input data is read from the file which will be made public a few dates before the due date. The format of the data is: one course per line, with a letter grade (string) and number of credits (integer) for each course, as shown in the example above. Test your program with your own data file.
3. Echo each (letter grade, credit) pair. That is, display the input data in a readable manner.
4. Also display the course points and the cumulative points.
5. Use a method to display the total number of credits and the overall GPA.

Here is my code so far:

I am currently getting the error message java.io.FileNotFoundException:
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at GPA.getCoursePoints(GPA.java:46)
at GPA.main(GPA.java:23)
any help would be greatly appreciated as this has me stuck!
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darrien Kamai wrote:I am currently getting the error message java.io.FileNotFoundException: ...
any help would be greatly appreciated as this has me stuck!

Because your getCoursePoints() method sets 'filename' to "", creates a File with it, and then tries to connect a Scanner to that.

I suspect you want some way of getting the output from your getFileName() method into your getCoursePoints() one; you just haven't done it right yet.

(BTW: If it was me, I think I'd have a getFile() method that returns a File, rather than passing Strings around, but it's up to you).

Winston

 
Darrien Kamai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks Winston that helped with the error. As far as a getfile() method i'm really not sure how to do that because my professor hasn't tought it and my book doesn't mention it. so far this is my code slightly revised.

We have to use Bluej as our program for writing and compiling and I think this might be it making a mistake (it frequently does) but I am now getting an error in the getCoursePoints() method, its saying that variable filename has not been initialized and that I should declare it. But if I do that then I am led back to my "" error that will stop it cold. This may be able to be fixed with the getfile() solution, any help on how to do that would be wonderful.
 
Darrien Kamai
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok finally figured it out, had to make more methods instead of 1 really complex one
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darrien Kamai wrote: . . . more methods instead of 1 really complex one
That is a valuable lesson to learn.

And welcome to the Ranch
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darrien Kamai wrote:Ok thanks Winston that helped with the error. As far as a getfile() method i'm really not sure how to do that because my professor hasn't tought it and my book doesn't mention it...

A quick peek ahead for you then :Strings are very powerful, but in this case you actually want a File, so it's usually better to return one.

The nice thing about doing it that way is that you can then also validate it without changing anything else, viz:Don't worry about it too much at the moment, but remember the message: Strings make a poor substitute for a proper class.

ok finally figured it out, had to make more methods instead of 1 really complex one

As Campbell said: a valuable lesson, and you should be proud that you've learnt it so quickly.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you really say while( f.exists() == false ) ...?
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Did you really say while( f.exists() == false ) ...?

Just for clarity, since we're in the Beginner's section; and I do sometimes use the construct if I have a lot of negative logic to deal with. Not a big fan of bangs, despite the Unix (and Lewes) background .

Winston
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume the dust has settled at Lewes, but getting the greenhorns used to there is a good idea. Otherwise you get them writing while(boo = false) ... by mistake.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic