May i know the bug, why my code cannot run

import java.util.Random;
public class Money {
    private int amount;
    static int[]notes;
    static int[]cents;
    private int[]amount_notes;
    private int[]amount_cents;
    private static Random rnd = new Random ();
    public Money(int amount){
    public Money(){
        final int[]notes={10000,5000,1000,500,100};
        final int[]cents={50,20,10,5};
        amount_notes = new int[notes.length];
        amount_cents = new int[cents.length];
    public int[] getAmount_notes() {
        return amount_notes;
    public int[] getAmount_cents() {
        return amount_cents;
    public void setAmount_notes(int[] amount_notes) {
        this.amount_notes = amount_notes;
    public void setAmount_cents(int[] amount_cents) {
        this.amount_cents = amount_cents;
    private void calc(){
        int temp = (int)(this.amount*100);
        for(int i=0;i<notes.length;i++){
            int count=temp/notes[i];
            temp -= notes[i]*count;
            amount_notes[i] = count;
        for(int i=0;i<cents.length;i++){
            int count=temp/notes[i];
            temp -= cents[i]*count;
            amount_cents[i] = count;
    public void display(){
        for(int i=0; i<amount_notes.length;i++){
            System.out.printf("RM %d: %d\n",notes[i]/100,amount_notes[i]);
        for(int i=0;i<amount_cents.length;i++){
            System.out.printf("RM %.2f: %d\n",(double)cents[i]/100,amount_cents[i]);
        for(int i=0;i<40;i++){

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I ran your code through an online compiler (jdoodle) which said you don’t have a main method. What are you doing with this code? Is this something you’re calling from another class? I need a bit more context. Otherwise try adding a main method
public static void main(String[] args)
and creating a demo object inside your main method. From the demo object you can call your various methods.

Hi I have run the code using tester class which contains main method but it still cannot work

What error are you getting? What is “not working”? I need more information.

This is because only one of your two constructors actually initializes the notes array. Depending on which constructor you call, your code can return an incompletely initialized object.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.