Java Help

<p>Ahhh I can’t understand this assignment. Please Help ? </p>

<p>Write a program that reads in letter grades for a class of students and calculates the class summary statistics. </p>

<p>a. The display should be formatted EXACTLY like the sample display below:</p>

<p>Enter grade #1: C
Enter grade #2: V
<v is=“” not=“” an=“” acceptable=“” grade=“”>
Enter grade #2: F
Enter grade #3: A
. . .</v></p>

<p>Enter a grade #15: A
Enter a grade #16: Z</p>

<pre><code> 15 students total
</code></pre>

<p>12 students passed: 8O.00%
3 students failed: 20.00%</p>

<p>b. The program should read in letter grades (A-F) from the user until the user enters a Z, signifying that he/she is done. (Z is referred to as the sentinel, or stopping value.)</p>

<p>c. If the letter grade is invalid (not A-F), the program should tell the user that the grade is unacceptable and prompt the user again. </p>

<p>d. The prompt to the user (Enter grade #1: ) should tell which grade number is being entered.</p>

<p>e. When all grades are entered, the program should display the following information as shown below:</p>

<p>• Total number of students
• Percent of students who passed
• Percent of students who failed</p>

<p>f. All percentages should have 2 digits to the right of the decimal.</p>

<p>g. The program should NEVER ask the user the total number of students.</p>

<hr>

<p>this is what I have so far. Am I even on the right track? </p>

<p>import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String args) {
Scanner reader = new Scanner (System.in);
double pass, fail, chicken;
char value;
pass = 0.0;
fail = 0.0 ;
chicken = 0.0;
System.out.println(“Enter your students’ grades. Enter Z when you are finished.”);
int cookie = 0;
while (chicken != 29){
cookie ++;
system.out.println("Please enter student " + cookie + “'s grade:”);
value = reader.nextchar();
char A,B,C,D,F,Z;
if (value == A ||value == B ||value == C||value == D){
pass ++;
}else if (value == F){
fail ++;
}else if (value == Z){
break;
}else{
system.out.println(“That is not a valid grade.”);
}
}

}}
</code></pre>

<h1>1. This might be just because you pasted it onto the website, but use tabs in your code. It’s hard to read without them.</h1>

<h1>2. In order to get two decimal places after each percentage, you’ll need to import DecimalFormat. You haven’t tried doing this yet, but it’s okay since it’s one of the later steps.</h1>

<h1>3. You have a lot of compile errors. Mostly typos, and some big ones.</h1>

<h1>4. The line:</h1>

<p>


value = reader.nextChar();

won’t work. The java.util.Scanner class doesn’t include a method called nextChar() (unfortunately, it really should). I actually can’t think of a way to read characters. I think your best bet for now would be to change those to strings, and read those (nextLine()).

5. This line:


if (value == A ||value == B ||value == C||value == D)

has four errors. I won’t tell you how to fix them, but think of what you’re comparing.

6. You will lose points on line 15 for using println.

7. After fixing all your errors and compiling the program, it looks like you've got yourself an infinite loop. The chicken thing you tried didn't do the trick, and neither did "Z". I'm not sure why after glancing at it, but something's wrong. I would reconsider using chicken at all to control whether the loop continues. And just out of curiosity, why did you choose 29? lol.

That's about it. Here's a more readable version for anyone else who wants to take a look:


import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    double pass, fail, chicken;
    char value;
    pass = 0.0;
    fail = 0.0 ;
    chicken = 0.0;
    System.out.println("Enter your students' grades. Enter Z when you are finished.");
    int cookie = 0;
    while (chicken != 29){
        cookie ++;
        system.out.println("Please enter student " + cookie + "'s grade:");
        value = reader.nextchar();
        char A,B,C,D,F,Z;
        if (value == A ||value == B ||value == C||value == D){
            pass ++;
            }
        else if (value == F){
            fail ++;
            }
        else if (value == Z){
            break;
            }
        else{
            system.out.println("That is not a valid grade.");
            }
        }
    }
</code></pre>

<p>}

</p>

<p>:)</p>

<h1>1. This might be just because you pasted it onto the website, but use tabs in your code. It’s hard to read without them.</h1>

<h1>2. In order to get two decimal places after each percentage, you’ll need to import DecimalFormat. You haven’t tried doing this yet, but it’s okay since it’s one of the later steps.</h1>

<h1>3. You have a lot of compile errors. Mostly typos, and some big ones.</h1>

<h1>4. The line:</h1>

<p>


value = reader.nextChar();

won’t work. The java.util.Scanner class doesn’t include a method called nextChar() (unfortunately, it really should). I actually can’t think of a way to read characters. I think your best bet for now would be to change those to strings, and read those (nextLine()).

5. This line:


if (value == A ||value == B ||value == C||value == D)

has four errors. I won’t tell you how to fix them, but think of what you’re comparing.

6. You will lose points on line 15 for using println.

7. After fixing all your errors and compiling the program, it looks like you've got yourself an infinite loop. The chicken thing you tried didn't do the trick, and neither did "Z". I'm not sure why after glancing at it, but something's wrong. I would reconsider using chicken at all to control whether the loop continues. And just out of curiosity, why did you choose 29? lol.

That's about it. Here's a more readable version for anyone else who wants to take a look:


import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    double pass, fail, chicken;
    char value;
    pass = 0.0;
    fail = 0.0 ;
    chicken = 0.0;
    System.out.println("Enter your students' grades. Enter Z when you are finished.");
    int cookie = 0;
    while (chicken != 29){
        cookie ++;
        system.out.println("Please enter student " + cookie + "'s grade:");
        value = reader.nextchar();
        char A,B,C,D,F,Z;
        if (value == A ||value == B ||value == C||value == D){
            pass ++;
            }
        else if (value == F){
            fail ++;
            }
        else if (value == Z){
            break;
            }
        else{
            system.out.println("That is not a valid grade.");
            }
        }
    }
</code></pre>

<p>}

</p>

<p>:)</p>

<h1>1. This might be just because you pasted it onto the website, but use tabs in your code. It’s hard to read without them.</h1>

<h1>2. In order to get two decimal places after each percentage, you’ll need to import DecimalFormat. You haven’t tried doing this yet, but it’s okay since it’s one of the later steps.</h1>

<h1>3. You have a lot of compile errors. Mostly typos, and some big ones.</h1>

<h1>4. The line:</h1>

<p>


value = reader.nextChar();

won’t work. The java.util.Scanner class doesn’t include a method called nextChar() (unfortunately, it really should). I actually can’t think of a way to read characters. I think your best bet for now would be to change those to strings, and read those (nextLine()).

5. This line:


if (value == A ||value == B ||value == C||value == D)

has four errors. I won’t tell you how to fix them, but think of what you’re comparing.

6. You will lose points on line 15 for using println.

7. After fixing all your errors and compiling the program, it looks like you've got yourself an infinite loop. The chicken thing you tried didn't do the trick, and neither did "Z". I'm not sure why after glancing at it, but something's wrong. I would reconsider using chicken at all to control whether the loop continues. And just out of curiosity, why did you choose 29? lol.

That's about it. Here's a more readable version for anyone else who wants to take a look:


import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    double pass, fail, chicken;
    char value;
    pass = 0.0;
    fail = 0.0 ;
    chicken = 0.0;
    System.out.println("Enter your students' grades. Enter Z when you are finished.");
    int cookie = 0;
    while (chicken != 29){
        cookie ++;
        system.out.println("Please enter student " + cookie + "'s grade:");
        value = reader.nextchar();
        char A,B,C,D,F,Z;
        if (value == A ||value == B ||value == C||value == D){
            pass ++;
            }
        else if (value == F){
            fail ++;
            }
        else if (value == Z){
            break;
            }
        else{
            system.out.println("That is not a valid grade.");
            }
        }
    }
</code></pre>

<p>}

</p>

<p>:)</p>

<h1>1. This might be just because you pasted it onto the website, but use tabs in your code. It’s hard to read without them.</h1>

<h1>2. In order to get two decimal places after each percentage, you’ll need to import DecimalFormat. You haven’t tried doing this yet, but it’s okay since it’s one of the later steps.</h1>

<h1>3. You have a lot of compile errors. Mostly typos, and some big ones.</h1>

<h1>4. The line:</h1>

<p>


value = reader.nextChar();

won’t work. The java.util.Scanner class doesn’t include a method called nextChar() (unfortunately, it really should). I actually can’t think of a way to read characters. I think your best bet for now would be to change those to strings, and read those (nextLine()).

5. This line:


if (value == A ||value == B ||value == C||value == D)

has four errors. I won’t tell you how to fix them, but think of what you’re comparing.

6. You will lose points on line 15 for using println.

7. After fixing all your errors and compiling the program, it looks like you've got yourself an infinite loop. The chicken thing you tried didn't do the trick, and neither did "Z". I'm not sure why after glancing at it, but something's wrong. I would reconsider using chicken at all to control whether the loop continues. And just out of curiosity, why did you choose 29? lol.

That's about it. Here's a more readable version for anyone else who wants to take a look:


import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    double pass, fail, chicken;
    char value;
    pass = 0.0;
    fail = 0.0 ;
    chicken = 0.0;
    System.out.println("Enter your students' grades. Enter Z when you are finished.");
    int cookie = 0;
    while (chicken != 29){
        cookie ++;
        system.out.println("Please enter student " + cookie + "'s grade:");
        value = reader.nextchar();
        char A,B,C,D,F,Z;
        if (value == A ||value == B ||value == C||value == D){
            pass ++;
            }
        else if (value == F){
            fail ++;
            }
        else if (value == Z){
            break;
            }
        else{
            system.out.println("That is not a valid grade.");
            }
        }
    }
</code></pre>

<p>}

</p>

<p>:)</p>

<p>oops. lol</p>

<p>Nice, I think UnleashedFury really nailed it. However, one thing I would change, if only because of habit, is to replace the break here:</p>

<p>



else if (value == Z){
                break;
}


with this:



else if(value==Z || value == z) { System.exit(1); }


</p>

<p>Why do you need help on making coffee???</p>

<p>you guys are amazing. thank you so much!</p>

<p>err, I don’t know how to put in the tabs. :
(our teacher wants us to use break – and also, the compile error message says “cannot find symbol method nextLine()” </p>

<p>here :
import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String args) {
TerminalIO.KeyboardReader reader = new TerminalIO.KeyboardReader();
double pass, fail, chicken;
String value;
pass = 0.0;
fail = 0.0 ;
chicken = 0.0;
System.out.print(“Enter your students’ grades. Enter Z when you are finished.”);
int cookie = 0;
String A,B,C,D,F,Z;
while (chicken != 29){
cookie ++;
System.out.println("Please enter student "+cookie+“s grade:”);
value = reader.nextLine();
if (value == A){

                pass ++; }
                else if (value == B){
                    pass ++;}
                    else if (value == C){
                        pass ++;
                    }else if (value == D){
                        pass ++; 
                    }
            else if (value == F){ 
                fail ++; 
            }else if (value == Z){
                break; 
            }else{
                System.out.println("That is not a valid grade."); 
            }
            }

}}
</code></pre>

<p>i chose 29 for the hell of it. and I’m getting rid of the chicken – I have a feeling that won’t be appreciated by others trying to make sense of this. I have no idea why the Z won’t work.</p>

<p>TerminalIO.KeyboardReader reader = new TerminalIO.KeyboardReader();</p>

<p>Where did that come from? I don’t recognize it, and if you don’t either, change it back to:
Scanner value = new Scanner(System.in)</p>

<p>Before I start again, in order to paste your code with the tabs, do this:
[noparse]


YOUR CODE HERE

[/noparse]

[quote] #5. This line:


Code:</p>

<p>if (value == A ||value == B ||value == C||value == D)

</p>

<p>has four errors. I won’t tell you how to fix them, but think of what you’re comparing.

[/quote]

You didn’t try to fix this? It’s really pretty simple. Let me give you a clue.</p>

<p>When the user inputs the grade, they put the letter “A”, not the number A. ;)</p>

<p>The reason nextLine() doesn’t work is because you have the wrong object in front of it. You actually never made the object to begin with. This goes back to that TerminalIO thing.</p>

<p>To get nextLine() to work, do this.</p>

<p>Change</p>

<p>TerminalIO.KeyboardReader reader = new TerminalIO.KeyboardReader();</p>

<p>back to </p>

<p>Scanner kb = new Scanner(System.in);</p>

<p>and then change </p>

<p>value = reader.nextLine();</p>

<p>to </p>

<p>value = kb.nextLine();</p>

<p>That’ll get the user input to work.</p>

<p>This is sort of random, but why are pass and fail doubles? You would probably be better off making those integers. </p>

<p>I see why the chicken thing isn’t ending the loop. You never put it in there! if you put</p>

<p>chicken++;</p>

<p>inside the while chicken != 29 loop, it’ll eventually stop.</p>

<p>You shouldn’t bother though, since you’re not supposed to do the loop 29 times. I recommend creating a boolean, and making it start out as true. Then, if “Z” is entered, loop will equal false, and you can type break; after that, and satisfy that requirement too.</p>

<p>[code ]</p>

<p>/**

  • @(#)GradeLoops.java
  • GradeLoops application
  • @author
  • @version 1.00 2008/8/17
    */</p>

<p>import java.util.Scanner;
public class GradeLoops {</p>

<pre><code>public static void main(String args) {
Scanner kb = new Scanner (System.in);
int pass;
int fail;
String grade;
grade = (“lawl”) ;
pass = 0;
fail = 0 ;
int cake = 42;
int cookie = 0;
while(cake == 42) {
cookie++;
System.out.print(“Enter student “+cookie+”'s grade:” );
grade=kb.nextLine();
if (grade.equals (“A”)||grade.equals(“B”)||grade.equals (“C”)||grade.equals(“D”)||grade.equals(“F”)||grade.equals(“Z”)){
if (grade.equals(“A”)||grade.equals(“B”)||grade.equals (“C”)||grade.equals(“D”)){
pass += 1;
}if (grade.equals(“F”)){
fail += 1; }
if (grade.equals(“Z”)){
} cookie–;
double percentpassed = ((pass/cookie)*100.00);
double percentfailed =((fail/cookie)*100.00);
System.out.println(pass+" students passed: “+percentpassed);
System.out.println(fail+” students failed: "+percentfailed);
cake ++;
}else { if (grade!= (“A”)||grade!=(“B”)||grade!=(“C”)||grade!=(“D”)||grade!=(“F”)||grade!=(“Z”))

                System.out.println("Not a valid grade. Stop being a bonehead."); 
                    cookie --; 

            }

            }           }

</code></pre>

<p>


 &lt;/p&gt;

&lt;p&gt;this is what i have so far, but it just keeps repeating "not a valid grade."&lt;/p&gt;

<p>wow, that failed. </p>

<p>Terminal IO is a keyboard reading application that our teacher made us download. I switched because I don’t know if it works any differently. I don’t understand why it’s not printing out a message asking the user to type in a grade; it automatically goes to the “else” without taking an input for grade. (I’m assuming it continues believing that grade = lawl ?) … but why</p>

<p>I’m not getting that error using the code you posted. BTW, it didn’t work because you had a space in [code ].</p>

<p>It’s [noparse]


, and then &lt;a href="you%20got%20the%20second%20one%20right"&gt;/code&lt;/a&gt;[/noparse].  &lt;/p&gt;

&lt;p&gt;I'm getting a divide by zero error. So somewhere your logic is wrong.&lt;/p&gt;