473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help with Java Grades. (While Loop String input)

Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far.


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import static java.lang.System.out;
  3.  
  4. public class Grades//Creates Class
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner myScanner = new Scanner(System.in);
  9.         String grade;
  10.         double counter1 = 0.0;//Variables
  11.         double counter2 = 0.0;
  12.         char i;        
  13.         char F;
  14.  
  15.         out.println("Enter your grade: ");
  16.         grade = myScanner.nextLine();
  17.  
  18.         while(grade != "q"){
  19.  
  20.         out.println("Enter your grade. Enter q to see whether or not you are eligible: ");
  21.         grade = myScanner.nextLine();
  22.  
  23.         if (grade == "A")
  24.          {
  25.             counter1 += 4.0;
  26.             counter2++;
  27.          }
  28.         else if (grade == "B")
  29.          {
  30.             counter1 += 3.0;
  31.             counter2++;
  32.          }
  33.         else if (grade == "C")
  34.          {
  35.             counter1 += 2.0;
  36.             counter2++;
  37.          }
  38.         else if (grade == "D")
  39.          {
  40.             counter1 += 1.0;
  41.             counter2++;
  42.          }
  43.         else if (grade == "F")
  44.          {
  45.             i = 'F';
  46.             counter1 += 0.0;
  47.             counter2++;
  48.           }
  49.  
  50.          }
  51.  
  52.         if (grade == "q"){
  53.  
  54.         if (counter2 < 4){
  55.             System.out.println("Ineligible, taking less than 4 classes");
  56.          }
  57.         else if ((counter2 >= 4) && (counter1/counter2 < 2.0)){
  58.             System.out.println("Ineligible, gpa below 2.0");
  59.          }
  60.         else if (i == 'F')
  61.         {
  62.             if ((counter2 >= 4) && (counter1 / counter2 < 2.0)){
  63.                 System.out.println("Inelegible, gpa below 2.0 and has F grade");
  64.             }
  65.             else ((counter2 >= 4) && (counter1 / counter2 >= 2.0)){
  66.                 System.out.println("Inelegible, gpa above 2.0 but has F grade");
  67.             }
  68.         }
  69.  
  70.         else{
  71.             System.out.println("GPA = " + (counter1/counter2) + " Eligible");
  72.          }
  73.         }
  74.    }    
  75.        }


It says stuff like 'i' is not initialized. But when I delete that line of code(I need it there though, just testing it), the string input thing won't stop when I enter q.
Nov 2 '07 #1
3 9631
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far.


import java.util.Scanner;
import static java.lang.System.out;

public class Grades//Creates Class
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
String grade;
double counter1 = 0.0;//Variables
double counter2 = 0.0;
char i;
char F;

out.println("Enter your grade: ");
grade = myScanner.nextLine();

while(grade != "q"){

out.println("Enter your grade. Enter q to see whether or not you are eligible: ");
grade = myScanner.nextLine();

if (grade == "A")
{
counter1 += 4.0;
counter2++;
}
else if (grade == "B")
{
counter1 += 3.0;
counter2++;
}
else if (grade == "C")
{
counter1 += 2.0;
counter2++;
}
else if (grade == "D")
{
counter1 += 1.0;
counter2++;
}
else if (grade == "F")
{
i = 'F';
counter1 += 0.0;
counter2++;
}

}

if (grade == "q"){

if (counter2 < 4){
System.out.println("Ineligible, taking less than 4 classes");
}
else if ((counter2 >= 4) && (counter1/counter2 < 2.0)){
System.out.println("Ineligible, gpa below 2.0");
}
else if (i == 'F')
{
if ((counter2 >= 4) && (counter1 / counter2 < 2.0)){
System.out.println("Inelegible, gpa below 2.0 and has F grade");
}
else ((counter2 >= 4) && (counter1 / counter2 >= 2.0)){
System.out.println("Inelegible, gpa above 2.0 but has F grade");
}
}

else{
System.out.println("GPA = " + (counter1/counter2) + " Eligible");
}
}
}
}



It says stuff like 'i' is not initialized. But when I delete that line of code(I need it there though, just testing it), the string input thing won't stop when I enter q.

All variables should be initialised when you decalare them.

ie int i = 0;

i will be changed later in your code but this would initialise the variable and keep your compiler happy :-)
Nov 2 '07 #2
All variables should be initialised when you decalare them.

ie int i = 0;

i will be changed later in your code but this would initialise the variable and keep your compiler happy :-)
Ah, I see now.

I'm also making another grades program, but it has a do while loop and a while loop. But I'm having problems with 'i'. See, when the loop runs, and the user enters 'i', I want 'i' to store the value 'F'. So in my while loop, if 'i' was equal to 'F', it would go to a different if-else statement.


import java.io.*;

Expand|Select|Wrap|Line Numbers
  1. public class Grade 
  2. {
  3.     public static void main(String[] args) throws IOException 
  4.     {
  5.       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  6.       String inData;
  7.       double grade = 0;
  8.       double counter1 = 0.0;
  9.  
  10.       do
  11.       {
  12.         System.out.println("Enter your grade. Enter q to quit");
  13.         inData = br.readLine();
  14.  
  15.          if (inData.equals("A"))
  16.          {
  17.            grade = grade + 4.0;
  18.            counter1++;
  19.          } 
  20.          else if (inData.equals("B"))
  21.          {
  22.            grade = grade + 3.0;
  23.            counter1++;
  24.          }
  25.          else if (inData.equals("C"))
  26.          {
  27.               grade = grade + 2.0;
  28.               counter1++;
  29.          }
  30.          else if (inData.equals("D"))
  31.          {
  32.            grade = grade + 1.0;
  33.            counter1++;
  34.          }
  35.          else if (inData.equals("F"))
  36.          {
  37.            grade = grade + 0.0;
  38.            counter1++;
  39.          }      
  40.     }
  41.  
  42.     while  (!inData.equals("q"));{
  43.  
  44.         if (counter1 < 4){
  45.             System.out.println("Ineligible, taking less than 4 classes");
  46.          }
  47.         else if ((counter1 >= 4) && (grade / counter1 < 2.0)){
  48.             System.out.println("Ineligible, gpa below 2.0");
  49.          }
  50.  
  51.         else{
  52.             System.out.println("GPA = " + (grade/counter1) + " Eligible");
  53.          }    
  54.         }
  55. }
  56. }
Nov 2 '07 #3
Ah, I see now.

I'm also making another grades program, but it has a do while loop and a while loop. But I'm having problems with 'i'. See, when the loop runs, and the user enters 'i', I want 'i' to store the value 'F'. So in my while loop, if 'i' was equal to 'F', it would go to a different if-else statement.


import java.io.*;

public class Grade
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inData;
double grade = 0;
double counter1 = 0.0;

do
{
System.out.println("Enter your grade. Enter q to quit");
inData = br.readLine();

if (inData.equals("A"))
{
grade = grade + 4.0;
counter1++;
}
else if (inData.equals("B"))
{
grade = grade + 3.0;
counter1++;
}
else if (inData.equals("C"))
{
grade = grade + 2.0;
counter1++;
}
else if (inData.equals("D"))
{
grade = grade + 1.0;
counter1++;
}
else if (inData.equals("F"))
{
grade = grade + 0.0;
counter1++;
}
}

while (!inData.equals("q"));{

if (counter1 < 4){
System.out.println("Ineligible, taking less than 4 classes");
}
else if ((counter1 >= 4) && (grade / counter1 < 2.0)){
System.out.println("Ineligible, gpa below 2.0");
}

else{
System.out.println("GPA = " + (grade/counter1) + " Eligible");
}
}
}
}
nvr mind. I found out the reason. Thank you so much for the help.
Nov 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lifeshortlivitup | last post by:
This program is supposed to calculate the number of grades entered and also how many a's, b's, c's, d's, and f's are entered. I have the number of grades part right but can't figure out how to fix...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
4
by: vadrama | last post by:
takes a list of grades and counts the amount A's, B's, C's etc... I am using a while statement to count the total number of grades which works great, but my if and else statements to count how...
2
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if...
1
by: tikney5 | last post by:
I need to modify my current program so that is uses a class to store and retrieve the employees name, hourly rate, and number of hourse worked. Use a constructor to initialize the employee...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
4
by: dacuteangel04 | last post by:
ok what my program is suppost to do is open a file - Grades.txt and read each line the breaking the lines down w/ tokens so i may later average some numbers an example some of the lines from the...
0
by: jac130 | last post by:
i need to populate an array with grades. there is an 'add students & grades' button. user is prompted by input box to enter a students name, then another input box to enter their grade. the student's...
2
by: jac130 | last post by:
I have an array, populated with student grades. there is a list of student names, but their grades are stored in a class level array. there is a calculate average button,that is supposed to calculate...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.