Hello Javanese,
I am new to this website, and I need some help with my code. Would appreciate it if anyone can help. I'm writing a code which displays the exam grades that students in a class achieve. My code produces five text input boxes labelled # of grade As, # of grade Bs, # of grade Cs, # grade Ds, # of grade Es. I then enter (for example) number 5 in each of the boxes. I then press a button labelled "Display bar chart", and I get the following on the screen: Grade As : 5, Grade Bs : 5, Grade Cs : 5, Grade Ds : 5, Grade Es : 5. I then press another button labelled "Clear the screen" and the data disappears. I then press a third button labelled "Display the letters", EXPECTING that I would get the following display:
Grade As : AAAAA, Grade Bs : BBBBB, Grade Cs : CCCCC, Grade Ds : DDDDD, Grade Es : EEEEE. In other words, instead of being represented by the number 5, I want the grades to be displayed as a string of five letters. Here's my problem : when I press the button "Display the letters", I do NOT get AAAAA, BBBBB, CCCCC, DDDDD, EEEEE. What I get is DOUBLE the letters (ie. TEN instead of five) I'm supposed to get, ie. I get AAAAAAAAAA, BBBBBBBBBB, CCCCCCCCCC, etc. It seems that when I press the "Clear the screen" button, I am NOT resetting the previous value, I'm simply CLEARING the screen and that's all. The PREVIOUS value of five is in fact being ADDED to the current value of five and as a result I'm getting a string of TEN letters (eg. AAAAAAAAAA) which is not what I want. Can anyone tell me what to do? I've thought about this for six or seven hours and I CAN'T come up with a solution.
My second (related) problem is that when I want to display the result as a string of 5 letters, I'm getting the word "null" followed by the string of letters, eg. I'm getting "Grade As : nullAAAAAAAAAA" instead of "AAAAA". Where is the "null" coming from??? Please could someone tell me what I'm doing wrong? I'm fairly new to Java. Here's my full code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GradeChart extends JFrame
implements ActionListener {
private JTextField gradeAField = new JTextField( 5 );
private JTextField gradeBField = new JTextField( 5 );
private JTextField gradeCField = new JTextField( 5 );
private JTextField gradeDField = new JTextField( 5 );
private JTextField gradeEField = new JTextField( 5 );
private int gradeAs;
private int gradeBs;
private int gradeCs;
private int gradeDs;
private int gradeEs;
String longStringA;
String gradeLetterA;
String longStringB;
String gradeLetterB;
String longStringC;
String gradeLetterC;
String longStringD;
String gradeLetterD;
String longStringE;
String gradeLetterE;
private JButton displayChartButton = new JButton( "Display bar chart" );
private JButton displayDataFieldsButton = new JButton( "Clear data fields" );
private JButton displayLongString = new JButton ("Display the letters");
private JButton clearScreen = new JButton ("Clear the screen");
private Color chartPanelColor = Color.cyan;
private final Font labelFont = new Font( "Serif", Font.BOLD, 16 );
private final int chartLeftXA = 30;
private final int chartLeftXB = 30;
private final int chartLeftXC = 30;
private final int chartLeftXD = 30;
private final int chartLeftXE = 30;
private final int chartTopYA = 50;
private final int chartTopYB = 102;
private final int chartTopYC = 154;
private final int chartTopYD = 206;
private final int chartTopYE = 258;
public static void main( String[] args ) {
GradeChart chart = new GradeChart();
chart.setSize( 550, 550 );
chart.createGUI();
chart.setVisible( true );
}
private void createGUI() {
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container window = getContentPane();
window.setLayout( new FlowLayout() );
JLabel gradeAFieldLabel = new JLabel( "# of grade As:" );
gradeAFieldLabel.setFont( labelFont );
window.add( gradeAFieldLabel );
window.add( gradeAField );
JLabel gradeBFieldLabel = new JLabel( "# of grade Bs:" );
gradeBFieldLabel.setFont( labelFont );
window.add( gradeBFieldLabel );
window.add( gradeBField );
JLabel gradeCFieldLabel = new JLabel( "# of grade Cs:" );
gradeCFieldLabel.setFont( labelFont );
window.add( gradeCFieldLabel );
window.add( gradeCField );
JLabel gradeDFieldLabel = new JLabel( "# of grade Ds:" );
gradeDFieldLabel.setFont( labelFont );
window.add( gradeDFieldLabel );
window.add( gradeDField );
JLabel gradeEFieldLabel = new JLabel( "# of grade Es:" );
gradeEFieldLabel.setFont( labelFont );
window.add( gradeEFieldLabel );
window.add( gradeEField );
displayChartButton = new JButton("Display bar chart");
window.add( displayChartButton );
displayChartButton.addActionListener( this );
displayDataFieldsButton = new JButton("Clear data fields");
window.add( displayDataFieldsButton );
displayDataFieldsButton.addActionListener( this );
displayLongString = new JButton("Display the letters");
window.add( displayLongString );
displayLongString.addActionListener( this );
clearScreen = new JButton("Clear the screen");
window.add( clearScreen );
clearScreen.addActionListener( this );
chartPanel.setPreferredSize( new Dimension( 450, 300 ) );
chartPanel.setBackground( chartPanelColor );
window.add( chartPanel );
}
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == displayChartButton ) {
checkAndRecordData();
Graphics g = chartPanel.getGraphics();
g.setColor(Color.white);
g.fillRect(20,20,410,52);
g.setColor(Color.black);
g.drawRect(20,20,410,52);
g.setColor( Color.black );
g.drawString( "Grade As: " + gradeAs, chartLeftXA, chartTopYA );
g.setColor(Color.white);
g.fillRect(20,72,410,52);
g.setColor(Color.black);
g.drawRect(20,72,410,52);
g.setColor( Color.black );
g.drawString( "Grade Bs: " + gradeBs, chartLeftXB, chartTopYB );
g.setColor(Color.white);
g.fillRect(20,124,410,52);
g.setColor(Color.black);
g.drawRect(20,124,410,52);
g.setColor( Color.black );
g.drawString( "Grade Cs: " + gradeCs, chartLeftXC, chartTopYC );
g.setColor(Color.white);
g.fillRect(20,176,410,52);
g.setColor(Color.black);
g.drawRect(20,176,410,52);
g.setColor( Color.black );
g.drawString( "Grade Ds: " + gradeDs, chartLeftXD, chartTopYD );
g.setColor(Color.white);
g.fillRect(20,228,410,52);
g.setColor(Color.black);
g.drawRect(20,228,410,52);
g.setColor( Color.black );
g.drawString( "Grade Es: " + gradeEs, chartLeftXE, chartTopYE );
}
if ( e.getSource() == displayDataFieldsButton ) {
gradeAField.setText("");
gradeBField.setText("");
gradeCField.setText("");
gradeDField.setText("");
gradeEField.setText("");
}
if (e.getSource() == clearScreen) {
checkAndRecordData();
Graphics g = chartPanel.getGraphics();
g.setColor(Color.white);
g.fillRect(20,20,410,52);
g.setColor(Color.black);
g.drawRect(20,20,410,52);
g.setColor( Color.black );
g.drawString( "", chartLeftXA, chartTopYA );
g.setColor(Color.white);
g.fillRect(20,72,410,52);
g.setColor(Color.black);
g.drawRect(20,72,410,52);
g.setColor( Color.black );
g.drawString( "", chartLeftXB, chartTopYB );
g.setColor(Color.white);
g.fillRect(20,124,410,52);
g.setColor(Color.black);
g.drawRect(20,124,410,52);
g.setColor( Color.black );
g.drawString( "", chartLeftXC, chartTopYC );
g.setColor(Color.white);
g.fillRect(20,176,410,52);
g.setColor(Color.black);
g.drawRect(20,176,410,52);
g.setColor( Color.black );
g.drawString( "", chartLeftXD, chartTopYD );
g.setColor(Color.white);
g.fillRect(20,228,410,52);
g.setColor(Color.black);
g.drawRect(20,228,410,52);
g.setColor( Color.black );
g.drawString( "", chartLeftXE, chartTopYE );
}
if (e.getSource() == displayLongString) {
Graphics g = chartPanel.getGraphics();
g.drawString( "Grade As: " + longStringA, 100, 50 );
g.drawString( "Grade Bs: " + longStringB, 100, 102 );
g.drawString( "Grade Cs: " + longStringC, 100, 154 );
g.drawString( "Grade Ds: " + longStringD, 100, 206 );
g.drawString( "Grade Es: " + longStringE, 100, 258 );
}
}
private void checkAndRecordData() {
int tempAs = 0;
int tempBs = 0;
int tempCs = 0;
int tempDs = 0;
int tempEs = 0;
tempAs = Integer.parseInt( gradeAField.getText() );
tempBs = Integer.parseInt( gradeBField.getText() );
tempCs = Integer.parseInt( gradeCField.getText() );
tempDs = Integer.parseInt( gradeDField.getText() );
tempEs = Integer.parseInt( gradeEField.getText() );
gradeAs = tempAs;
gradeBs = tempBs;
gradeCs = tempCs;
gradeDs = tempDs;
gradeEs = tempEs;
String gradeLetterA = "A";
for (int i=0; i<tempAs; i++)
longStringA += gradeLetterA;
String gradeLetterB = "B";
for (int i=0; i<tempBs; i++)
longStringB += gradeLetterB;
String gradeLetterC = "C";
for (int i=0; i<tempCs; i++)
longStringC += gradeLetterC;
String gradeLetterD = "D";
for (int i=0; i<tempDs; i++)
longStringD += gradeLetterD;
String gradeLetterE = "E";
for (int i=0; i<tempEs; i++)
longStringE += gradeLetterE;
}
}