Friday, July 31, 2009

JAVA Making a CHAR equal a Number for an exercise creating GRADEBOOK. 2nd Post with CODE?

Possibly have MANY errors, but for now I'm concerned with the IF statement:


import javax.swing.*;


import java.lang.String;


import java.io.*; // This imports the File class


public class Lab10


{


public static void main (String args[])throws Exception


{


String StudentCountStr, SnameStr,Snumr,courseStr,lgradeStr,Grade...


Double nGrade;


FileWriter fw = new FileWriter("GradesTable.txt");


BufferedWriter bw = new BufferedWriter(fw);


PrintWriter pw = new PrintWriter(bw);


customerCountStr = JOptionPane.showInputDialog(null, "How many Customers to enter?");


StudentCountStr = JOptionPane.showInputDialog(null, "How many Student Grades to enter?");


/* Here the instructor will enter the number of students that must be entered.


Below, the string will be parsed to a numeric value for calculations */


SnameStr = JOptionPane.showInputDialog(null, "Enter Student Name");


Snumr = JOptionPane.showInputDialog(null, "Enter Student Number");


courseStr = JOptionPane.showInputDialog(null, "Enter Course Name %26amp; Section:");


lgradeStr = JOptionPane.showInputDialog(null, "Enter Letter grade: (no + or -");





// Grades are assigned as follows: A=4,B=3,C=2,D=1,F=0


Double GradeCount = Double.valueOf(GradeCountStr);





/* The syntax for the FOR loop is:


for(%26lt;initialization%26gt;; %26lt;condition%26gt;; %26lt;update%26gt;)


{ %26lt;executable statements%26gt; } */


for(int loopCount = 1; loopCount %26lt;= GradeCount; ++loopCount)





/* Here loopCount is initiated to 1 so as to never have a ZERO at the beginning. Then it is compared to


GradeCount. If loopCount is less than or equal to GradeCount, the program will go on %26amp; 1 will be added


to the value of loopcount. When loopCount is GREATER than GradeCount, the program will ask if there's


another student, if not it'll end.*/


{


if(lgradeStr.equals('A')) {nGrade = 4;}


else if (lgradeStr.equals(‘B’)) { nGrade = 3; }


else if (lgradeStr.equals(‘C’)) { nGrade = 2; }


else if (lgradeStr.equals(‘D’)) { nGrade = 1; }


else {ngrade = 0;}}


pw.println(" " + lgrade + " " + Gradepoints);


{


}


{


pw.println("Bucks County Community College Grade Report For");//this places the heading


pw.println(SnameStr);


pw.println(Snumr);


pw.println(" ");


pw.println(" ");


pw.println(" Course " + " Grade "+"Gradepoints");//This places the SubHeadings


}


pw.close();


}


}

JAVA Making a CHAR equal a Number for an exercise creating GRADEBOOK. 2nd Post with CODE?
This is quite simple, just use an if statement.





something like this:








char a = in.nextChar();


char b = 'b'


int i = (anything);


if(a == b)


{


i = 1;


}





and so on
Reply:I'm not to fmiliar with Java, but it sounds like you need to compare 2 strings (declare nGrade as a variable) to display 4 for A, 3 for B.... but that won't get you the GPA becaus eyou won't be able to ADD. Sorry


No comments:

Post a Comment