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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    #1

    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.


    [CODE=java]import java.util.Scann er;
    import static java.lang.Syste m.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("En ter your grade: ");
    grade = myScanner.nextL ine();

    while(grade != "q"){

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

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

    else{
    System.out.prin tln("GPA = " + (counter1/counter2) + " Eligible");
    }
    }
    }
    }[/CODE]



    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.
    Last edited by Ganon11; Nov 2 '07, 01:23 PM. Reason: Please use the [CODE] tags provided.
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #2
    Originally posted by Energizer100
    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.Scann er;
    import static java.lang.Syste m.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("En ter your grade: ");
    grade = myScanner.nextL ine();

    while(grade != "q"){

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

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

    else{
    System.out.prin tln("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 :-)

    Comment

    • Energizer100
      New Member
      • Nov 2007
      • 60

      #3
      Originally posted by Dameon99
      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.*;

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

      do
      {
      System.out.prin tln("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.prin tln("Ineligible , taking less than 4 classes");
      }
      else if ((counter1 >= 4) && (grade / counter1 < 2.0)){
      System.out.prin tln("Ineligible , gpa below 2.0");
      }

      else{
      System.out.prin tln("GPA = " + (grade/counter1) + " Eligible");
      }
      }
      }
      }[/CODE]
      Last edited by Ganon11; Nov 2 '07, 01:24 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • Energizer100
        New Member
        • Nov 2007
        • 60

        #4
        Originally posted by Energizer100
        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 InputStreamRead er(System.in));
        String inData;
        double grade = 0;
        double counter1 = 0.0;

        do
        {
        System.out.prin tln("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.prin tln("Ineligible , taking less than 4 classes");
        }
        else if ((counter1 >= 4) && (grade / counter1 < 2.0)){
        System.out.prin tln("Ineligible , gpa below 2.0");
        }

        else{
        System.out.prin tln("GPA = " + (grade/counter1) + " Eligible");
        }
        }
        }
        }
        nvr mind. I found out the reason. Thank you so much for the help.

        Comment

        Working...