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

Page 1 of 1

5 Replies - 4003 Views - Last Post: 11 January 2009 - 08:50 PM Rate Topic: -----

#1 javakillsme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:10 PM

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:




 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 January 2009 - 08:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: GPA Program - Takes user inputs and calculates GPA

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:28 PM

Pleas edit your post above to add the code tags:

:code:
Was This Post Helpful? 0
  • +
  • -

#3 javakillsme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

Re: GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:31 PM

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:




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.");
}
}


} 


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:35 PM

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



Duplicate topics
Topics merged
Please avoid duplicate posting :angry: :angry:
Was This Post Helpful? 0
  • +
  • -

#5 javakillsme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

Re: GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:41 PM

View Postpbl, on 11 Jan, 2009 - 07:35 PM, said:

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



Duplicate topics
Topics merged
Please avoid duplicate posting :angry: :angry:


will the tokenizer work with chn.util.* or do i need to import another library?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: GPA Program - Takes user inputs and calculates GPA

Posted 11 January 2009 - 08:50 PM

View Postjavakillsme, on 11 Jan, 2009 - 07:41 PM, said:

View Postpbl, on 11 Jan, 2009 - 07:35 PM, said:

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



Duplicate topics
Topics merged
Please avoid duplicate posting :angry: :angry:


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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1