Results 1 to 3 of 3
  1. #1
    zafri is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default why public void clear() had error pls Help mE

    hi there
    this is my project
    y if i run in NEtBeans
    got some problem at public void clear, but the program still can run , an output process still can use ,
    pls someone help me..
    i got presentation on 02-oct-2012 , 12.am-1.pm Malaysian time

    ///////////////////////////////////
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import javax.swing.*;
    public class GpaCalculator2 extends JFrame // within frame of window
    implements ActionListener { // for buttons, textfields,...
    private static final int GPA = 20;
    private static float[] creditsArray = new float[GPA];
    private static float[] gradesArray = new float[GPA];
    // String

    // textfields
    private JTextField nameField, creditsField, gradeField, gpaField, statusField;


    // buttons
    private JButton updateButton, calculateButton, clearButton, exitButton,
    helpButton;
    // set up GUI
    public GpaCalculator2()
    {
    super( "GPA Calculator" ); // title


    Container container = getContentPane(); // frame
    container.setLayout( new GridLayout( 6, 4 ) ); // layout within frame


    container.add(
    new JLabel( "Student Name",SwingConstants.CENTER ) );
    nameField = new JTextField( 10 );
    container.add( nameField );
    nameField.addActionListener( this );

    // Credits label and text input field
    container.add(
    new JLabel( "Class Credits(hours)",SwingConstants.CENTER ) );
    creditsField = new JTextField( 10 );
    container.add( creditsField );
    creditsField.addActionListener( this);


    // Grade label and text input field
    container.add(
    new JLabel( "Grade for class",SwingConstants.CENTER ) );
    gradeField = new JTextField( 10 );
    container.add( gradeField );
    gradeField.addActionListener( this );

    // GPA label and text ouput field
    container.add(
    new JLabel( "Current GPA",SwingConstants.CENTER ) );
    gpaField = new JTextField( 10 );
    gpaField.setEditable( false );
    container.add( gpaField );

    // Status of current input
    statusField = new JTextField( 15 );
    statusField.setEditable( false );
    container.add( statusField );

    // create and add buttons

    calculateButton = new JButton( "CALCULATE" );

    updateButton = new JButton( "UPDATE" );

    clearButton = new JButton( "CLEAR" );





    container.add( updateButton );

    updateButton.addActionListener( this );

    container.add( calculateButton );

    calculateButton.addActionListener( this );

    container.add( clearButton );

    clearButton.addActionListener( this );





    // size of window, as seen by user
    setSize( 800, 400 );
    setVisible( true );
    }

    // what happens when an object that has an ActionListener is clicked, inputed,...
    public void actionPerformed( ActionEvent event ) {
    DecimalFormat precision2 = new DecimalFormat( "0.00" );
    // update button listener
    if ( event.getSource() == updateButton ) {
    if ( !( gradeField.getText().equals( "" ) ) || !( creditsField.getText().equals( "" ) ) ) {
    for (int i = 0; i < gradesArray.length; i++) {
    if ( ( gradesArray[ i ] == -999 ) && ( creditsArray[ i ] == -999 ) ) {

    try {
    gradesArray[ i ] = Float.parseFloat( gradeField.getText() );
    creditsArray[ i ] = Float.parseFloat( creditsField.getText() );
    statusField.setText("StuName : "+(" ")+ " Grade: " + gradeField.getText()+(" ") + " Credits: " + creditsField.getText() );
    creditsField.setText( " " );
    gradeField.setText( " " );
    nameField.setText(" ");


    }
    catch ( NumberFormatException numberformat ) {
    JOptionPane.showMessageDialog( null,
    "Type in numbers for both fields",
    "ERROR", JOptionPane.ERROR_MESSAGE );
    if ( !gradeField.getText().equals( "" ) ) {
    System.out.print( " Grade: " + gradeField.getText() );
    }
    if ( !creditsField.getText().equals( "" ) ) {
    System.out.print( " Credits: " + creditsField.getText() );
    }
    }
    catch ( Exception numberformat ) {
    JOptionPane.showMessageDialog( null, "Please input a number in both fields","NUMBER FORMAT ERROR", JOptionPane.ERROR_MESSAGE );
    }
    break;
    }

    else if ( GPA == gradesArray.length ) {
    clear();

    }
    }
    }
    }

    // calculate button listener
    else if ( event.getSource() == calculateButton ) {
    float total_credits = 0;
    float total_grade = 0;
    for ( int i = 0; i < creditsArray.length; i++ ) {
    if ( creditsArray[ i ] != -999 ) {
    total_credits += creditsArray[ i ];
    }
    }
    for ( int i = 0; i < gradesArray.length; i++ ) {
    if ( gradesArray[ i ] != -999 ) {
    float gradesTemp = gradesArray[ i ];
    float creditsTemp = creditsArray[ i ];
    float gpa;
    gpa = creditsTemp / total_credits * gradesTemp;
    total_grade = total_grade + gpa;
    }
    }
    gpaField.setText( "" + precision2.format( total_grade ) );
    }
    // clear button listener get input then clear all
    else if ( event.getSource() == clearButton ) {
    clear();

    creditsField.setText("");
    gradeField.setText("");
    nameField.setText(" ");
    }

    // Clear all
    public void clear() {
    for ( int i = 0; i < gradesArray.length; i++ ) {
    gradesArray[ i ] = -999;
    creditsArray[ i ] = -999;

    }
    statusField.setText("");
    gpaField.setText("");

    }
    // execute application
    public static void main( String args[] )
    {
    GpaCalculator2 application = new GpaCalculator2();

    application.setDefaultCloseOperation(
    JFrame.EXIT_ON_CLOSE );

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: why public void clear() had error pls Help mE

    If your code is producing errors and you need help, it's wise to post the errors so that we know what they are.

    Also please wrap your code in code tags so that it is readable. Please read the links in my signature below to see how to do this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: why public void clear() had error pls Help mE

    Considering they were asking this question at 9.00 MYT, 3 hours before their presentation, the odds of anyone managing to answer that were pretty small.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 09-27-2011, 03:59 PM
  2. Replies: 10
    Last Post: 07-10-2011, 08:37 AM
  3. What wrong with it?case : public static void main
    By bytescode in forum New To Java
    Replies: 7
    Last Post: 02-10-2011, 07:01 AM
  4. Replies: 1
    Last Post: 12-04-2010, 05:41 PM
  5. Replies: 9
    Last Post: 11-22-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •