Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need help with a program I`ve written......

Options
  • 10-05-2010 5:03am
    #1
    Registered Users Posts: 153 ✭✭


    Hi there,

    Not sure if this is the right place for this so mods feel free to move as appropriate.
    Im having major issues with a program Ive written for a college assigment due in tommorow. It`s compilling without errors but when I try and run it it says:Exception in thread "main" java.lang.NoSuchMethodError: main
    Press any key to continue . . .


    If there are any programmers about, could you have a look over this code and advise me where I`ve gone wrong.....under pressure to have this soon and havent got a clue why it won`t run.

    Thanks,

    Ruth
    import java.io.*;
    import java.awt.*;
    import java. awt.event.*;

    public class CreditUnion
    {
    private int account;
    private String lastName;
    private String firstName;
    private double balance;

    //read a record from the specified RandomAccessFile
    public void read(RandomAccessFile file) throws IOException
    {
    account=file.readInt();

    char first[] =new char[ 15 ];

    for (int i=0; i < first.length;i++)
    first [ i ]=file.readChar();

    firstName =new String ( first );

    char last[]= new char [ 15 ];

    for (int i=0; i < last.length; i++)
    last [ i ]=file.readChar();

    lastName =new String( last);



    balance=file.readDouble();

    }

    //write a record to the specified RandomAccessFile
    public void write( RandomAccessFile file ) throws IOException
    {
    StringBuffer buf;

    file.writeInt( account );

    if (firstName !=null )
    buf= new StringBuffer( firstName );
    else
    buf =new StringBuffer( 15 );

    buf.setLength( 15 );

    file.writeChars( buf.toString() );

    if (lastName != null)
    buf =new StringBuffer( lastName );
    else
    buf= new StringBuffer( 15 );

    buf .setLength( 15 );

    file.writeChars( buf.toString() );

    file.writeDouble( balance );
    }

    public void setAccount( int a ) {account = a;}
    public int getAccount() {return account; }
    public void setfirstName( String f ) {firstName =f;}
    public String getfirstName() {return firstName;}
    public void setlastName( String l ) {lastName =l;}
    public String getlastName() {return lastName;}
    public void setBalance( double b ) {balance =b;}
    public double getBalance() {return balance; }

    //Note: This method contains a hard coded value for the
    //size of a record of information
    public static int size() { return 72;}
    }


Comments

  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    You haven't defined a main() method to call your other methods.
    Eg.
    public static void main(String[] args)
    {
        try
        {
        CreditUnion instance = new CreditUnion();
    
        File f = new File("CreditUnion.dat");
        RandomAccessFile raf = new RandomAccessFile(f, "rw"); 
    
         // set the account no, name etc
    
        instance.write(raf);
    
        instance.read(raf);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    
    }
    


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    As per CreepingDeath's post, you can't run programs in Java without some kind of main() method for them to start from. See here;
    http://java.sun.com/docs/books/tutorial/getStarted/application/index.html#MAIN


  • Registered Users Posts: 153 ✭✭Ruthie-Roux


    As per CreepingDeath's post, you can't run programs in Java without some kind of main() method for them to start from. See here;
    http://java.sun.com/docs/books/tutorial/getStarted/application/index.html#MAIN


    Thats what I thought. But Im working off examples like below which do run perfectly without public static void main etc.

    import java.io.*;
    import java.awt.*;
    import java. awt.event.*;
    //Import packages

    public class WriteRandomFile extends Frame
    implements ActionListener
    {


    private TextField numberField,NameField,

    withdrawalField, balanceField;
    //TextField where user enters account info


    private Button enter,
    //send record to file
    done;
    //quit program



    // Application other pieces
    private RandomAccessFile output ;
    // file for output
    private Record data;


    public WriteRandomFile ()
    //This constructor initializes the frame
    {
    super("Write to random access file");
    data = new Record();

    //Open the file

    try {
    output = new RandomAccessFile( "credit.dat","rw");

    }

    catch (IOException e) {
    System.err.println(e .toString() );
    System.exit(1);
    }

    setSize(300,150);
    setLayout( new GridLayout(5,2) );

    //Create the new components of the Frame
    add( new Label( " Number") ) ;
    numberField = new TextField ();
    add(numberField);


    add ( new Label(" Name " ) ) ;
    NameField= new TextField( 20);
    add (NameField);

    add( new Label ( "Current Balance ") ) ;
    balanceField = new TextField( 20);
    add(balanceField);


    add( new Label ( " Withdrawal limit ") ) ;
    withdrawalField = new TextField( 20);
    add(withdrawalField);

    enter = new Button ( "Enter ");
    enter.addActionListener(this);
    add(enter);

    done = new Button ("Done ");
    done.addActionListener(this);
    add(done);

    setVisible(true);

    }

    public void addRecord()
    {
    int Number = 0;
    Double d ;

    if (! numberField.getText().equals("") ) {




    //output the values to the file
    try {

    Number =
    Integer.parseInt(accountField.getText() );


    if ( Number> 0 && Number <= 100) {

    data.setAccount( accountNumber);
    data.setName(NameField.getText() );
    data.setWithdrawal(WithdrawalField.getText() );
    d = new Double (balanceField.getText() );
    data.setBalance( d.doubleValue () );

    output. seek(
    ( long) (Number-1) * Record.size() );
    data.write(output);
    }


    //clear the textfields
    accountField.setText(" ");
    firstNameField.setText("");

    lastNameField.setText("");
    balanceField.setText("");
    }

    catch (NumberFormatException nfe) {

    System.err.println(
    " you must enter an integer account number");
    }

    catch (IOException io) {
    System.err.println(
    "Error during write to file\n"+
    io.toString() );
    System.exit(1);
    }
    }
    }

    public void actionPerformed(ActionEvent e )
    {
    addRecord();

    if ( e.getSource() ==done) {
    try {
    output.close();
    }
    catch(IOException io) {
    System.err.println("File not closed properly\n"+ io.toString());
    }
    System.exit(0);
    }
    }

    //Instantiate a writeRandomFile object and start the program
    public static void main (String args[] )
    {
    new WriteRandomFile();
    }
    }


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    Thats what I thought. But Im working off examples like below which do run perfectly without public static void main etc.

      //Instantiate a writeRandomFile object and start the program
      public static void main (String args[] )
      {
    	 new WriteRandomFile();
       }
    }
    

    That example does have a main method.


    You have to have a main method "somewhere", and start your program from that class.
    Either
    a) have the main method within the CreditUnion class or
    b) create a separate class eg. CreditUnionApplication which has a main method and calls the CreditUnion class methods.


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    Your example does have a main() method - down the very end of the code you posted :)

    Edit - beaten to it :)


  • Advertisement
  • Registered Users Posts: 153 ✭✭Ruthie-Roux


    Oh so it does :rolleyes:

    Where is my head today? Thanks folks, I really should have seen that.
    Need coffee. Yes. More coffee.

    Ruth


Advertisement