Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,303 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 780 people online right now. Registration is fast and FREE... Join Now!




GPA Program - Takes user inputs and calculates GPA

 
Reply to this topicStart new topic

GPA Program - Takes user inputs and calculates GPA, I can't get my program to read more than one token at a time. I ne

javakillsme
11 Jan, 2009 - 07:10 PM
Post #1

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 3

How do I get the program to read more than one letter at a time.
Ex. Enter your grades: (user enters A B D F)
I need to read each letter separately and then give the user their gpa and a reply based on their gpa and grades.

If the user has less than for grades they are ineligible.
If the user has no F's and a GPA above 2.0 they are eligible.
If the user has an F and a GPA above 2.0 they are ineligible.
If the user has no F's but a GPA below 2.0 they are ineligible.
It the user has an F and a GPA below 2.0 they are ineligible.

this is what I have:




CODE

import chn.util.*;

class gpaTest
{
    public static void main(String[] args)
    {
        int courses;
        String grade;
        int totalGrade;
        double points;
        char letterGrade;

        totalGrade = 0;
        points = 0.0;

        ConsoleIO keyboard = new ConsoleIO();
        
        //number of courses
        System.out.print("Please enter the number of courses in which you are enrolled: ");
        courses = keyboard.readInt();
        System.out.println();
        
        if(courses >= 4)
        {
            int counter1 = 0;
        do
        {

            
          
               System.out.print("Please enter your grades: ");
              grade = keyboard.readLine();
              int counter2 = 0;
            letterGrade = grade.charAt(counter2++);


            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("Invalid letter grade, try again.");
                counter1--;
            }
        
                    
                double GPA = (totalGrade / (double)courses);
    
            if(GPA < 2.0 && (letterGrade != 'f' || letterGrade != 'F'))
            {
                System.out.println();
                System.out.println("GPA = " + GPA + " Ineligible, gpa below 2.0");
                System.out.println();
            }
            else if (GPA >= 2.0 && (letterGrade == 'f' || letterGrade == 'F'))
            {
                System.out.println();
                System.out.println("GPA = " + GPA + " Ineligible, gpa above 2.0 but has F grade");
                System.out.println();
            }
            else if (GPA < 2.0 && (letterGrade == 'f' || letterGrade == 'F'))
            {
                System.out.println();
                System.out.println("GPA = " + GPA + " Ineligible, gpa below 2.0 and has F grade");
                System.out.println();
               }
            else if (GPA >= 2.0 && letterGrade != 'f' || letterGrade != 'F')
                {
                System.out.println();
                System.out.println("GPA = " + GPA + " Eligible");
                System.out.println();
                }
            
            }while (counter1 <= courses);
}
            else
        {
            System.out.print("Ineligible, taking less than 4 classes.");
        }
    }
        
        
}


*Edited [ code] tags added

This post has been edited by pbl: 11 Jan, 2009 - 07:30 PM

User is offlineProfile CardPM
+Quote Post


n8wxs
RE: GPA Program - Takes User Inputs And Calculates GPA
11 Jan, 2009 - 07:28 PM
Post #2

--... ...-- -.. . -. ---.. .-- -..- ...
Group Icon

Joined: 6 Jan, 2008
Posts: 1,609



Thanked: 223 times
My Contributions
Pleas edit your post above to add the code tags:

code.gif

User is offlineProfile CardPM
+Quote Post

javakillsme
RE: GPA Program - Takes User Inputs And Calculates GPA
11 Jan, 2009 - 07:31 PM
Post #3

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 3

How do I get the program to read more than one letter at a time.
Ex. Enter your grades: (user enters A B D F)
I need to read each letter separately and then give the user their gpa and a reply based on their gpa and grades.

If the user has less than for grades they are ineligible.
If the user has no F's and a GPA above 2.0 they are eligible.
If the user has an F and a GPA above 2.0 they are ineligible.
If the user has no F's but a GPA below 2.0 they are ineligible.
It the user has an F and a GPA below 2.0 they are ineligible.

this is what I have:



CODE


import chn.util.*;

class gpaTest
{
public static void main(String[] args)
{
int courses;
String grade;
int totalGrade;
double points;
char letterGrade;

totalGrade = 0;
points = 0.0;

ConsoleIO keyboard = new ConsoleIO();

//number of courses
System.out.print("Please enter the number of courses in which you are enrolled: ");
courses = keyboard.readInt();
System.out.println();

if(courses >= 4)
{
int counter1 = 0;
do
{



System.out.print("Please enter your grades: ");
grade = keyboard.readLine();
int counter2 = 0;
letterGrade = grade.charAt(counter2++);


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("Invalid letter grade, try again.");
counter1--;
}


double GPA = (totalGrade / (double)courses);

if(GPA < 2.0 && (letterGrade != 'f' || letterGrade != 'F'))
{
System.out.println();
System.out.println("GPA = " + GPA + " Ineligible, gpa below 2.0");
System.out.println();
}
else if (GPA >= 2.0 && (letterGrade == 'f' || letterGrade == 'F'))
{
System.out.println();
System.out.println("GPA = " + GPA + " Ineligible, gpa above 2.0 but has F grade");
System.out.println();
}
else if (GPA < 2.0 && (letterGrade == 'f' || letterGrade == 'F'))
{
System.out.println();
System.out.println("GPA = " + GPA + " Ineligible, gpa below 2.0 and has F grade");
System.out.println();
}
else if (GPA >= 2.0 && letterGrade != 'f' || letterGrade != 'F')
{
System.out.println();
System.out.println("GPA = " + GPA + " Eligible");
System.out.println();
}

}while (counter1 <= courses);
}
else
{
System.out.print("Ineligible, taking less than 4 classes.");
}
}


}

User is offlineProfile CardPM
+Quote Post

pbl
RE: GPA Program - Takes User Inputs And Calculates GPA
11 Jan, 2009 - 07:35 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
CODE

String line = whateverToolYouUse.readLine();
StringTokenizer st = new StringTokenizer(line);
while(st.hasNext() {
    String letter = st.next();
    ....
}


Duplicate topics
Topics merged
Please avoid duplicate posting mad.gif mad.gif

User is online!Profile CardPM
+Quote Post

javakillsme
RE: GPA Program - Takes User Inputs And Calculates GPA
11 Jan, 2009 - 07:41 PM
Post #5

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 3

QUOTE(pbl @ 11 Jan, 2009 - 07:35 PM) *

CODE

String line = whateverToolYouUse.readLine();
StringTokenizer st = new StringTokenizer(line);
while(st.hasNext() {
    String letter = st.next();
    ....
}


Duplicate topics
Topics merged
Please avoid duplicate posting mad.gif mad.gif


will the tokenizer work with chn.util.* or do i need to import another library?
User is offlineProfile CardPM
+Quote Post

pbl
RE: GPA Program - Takes User Inputs And Calculates GPA
11 Jan, 2009 - 07:50 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 6,954



Thanked: 673 times
Dream Kudos: 200
My Contributions
QUOTE(javakillsme @ 11 Jan, 2009 - 07:41 PM) *

QUOTE(pbl @ 11 Jan, 2009 - 07:35 PM) *

CODE

String line = whateverToolYouUse.readLine();
StringTokenizer st = new StringTokenizer(line);
while(st.hasNext() {
    String letter = st.next();
    ....
}


Duplicate topics
Topics merged
Please avoid duplicate posting mad.gif mad.gif


will the tokenizer work with chn.util.* or do i need to import another library?


Don't know what chn.util is for StringTokenizer will split a String between spaces and tab
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:54PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month