0

I have just started learning Java as part of my university course and am having a problem with y first project. I am just starting to create a project that basically sorts coins. I am trying to make a method called printCoinList() that prints the contents of a coin list, indicating what denominations are currently in circulation (i.e "The current coin denominations are in circulation: 200,100,50,20,10), in pennies.

So far I have declared my instance fields, created a parameter and attempted to make this method. My only issue is when i try and test it in the main() method it seems to have a problem with me using an array as the coinList parameter. This is what I have so far:

public class CoinSorter {

    //Instance Fields
    String currency;
    int minCoinIn;
    int maxCoinIn;
    int[] coinList;
    
    //constructor
    public CoinSorter(String Currency, int minValueToExchange, int maxValueToExchange, int[] initialCoinList) {
        currency=Currency;
        minCoinIn=minValueToExchange;
        maxCoinIn = maxValueToExchange;
        coinList= initialCoinList;
        
    }
    
                public void printCoinList() {
        System.out.println("The current coin denominations are in circulation"
                + coinList);
    }
    
    
    public static void main(String[] args) {
        //An example
        
        CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200});

The only problems seems to be in exampleOne as when I take this out the rest of the code seems to run fine. The error message is:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The constructor CoinSorter(String, int, int, int, int, int, int, int) is undefined
    Syntax error on token "{", delete this token
    Syntax error on token "}", delete this token

So does anyone know what I am doing wrong?

4 Answers 4

1

This is because an array initializer may only be specified at declaration site or as part of an array creation expression. (JLS § 10.6)

Below is an array initializer at declaration site.

int[] array = { 2, 3, 5, 7, 11 };

This is short for

int[] array = new int[] { 2, 3, 5, 7, 11 };

However, it may not be used as 'array literal', unlike a string literal. That means that you must write out the array creation expression:

new CoinSorter("pounds", 0, 10000, new int[] { 10, 20, 50, 100, 200 });
Sign up to request clarification or add additional context in comments.

4 Comments

Hello thanks! that has worked. I used new CoinSorter("pounds", 0, 10000, new int[] { 10, 20, 50, 100, 200 }); However now when i try to print my method it comes out with The current coin denominations are in circulation[I@1c4af82c Instead of printting the coin denominations, it just prints the memory. Any idea how to fix this as well? Thanks very much!
@Millyfg That question is already answered here.
Thank you! all sorted.
@Millyfg Don't forget to mark one of the answers as accepted, to let the community know that this question has been addressed.
1

Arrays in java can be declared/initialized using one of the following ways.

int[] myIntArray = {10,20,50,100,200};
int[] myIntArray = new int[]{10,20,50,100,200};

replace CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200});

with

  CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, myIntArray );

OR

   CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, new int[]{10,20,50,100,200});

Comments

0

Firt of all , in java , you need to specify the type of your Array :

CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, new int[]{10,20,50,100,200});

Then your "printCoinList" method will not work as excepted , this should print :

The current coin denominations are in circulation [I@7852e922

Your final code should be :

import java.util.Arrays;

public class CoinSorter {

//Instance Fields
String currency;
int minCoinIn;
int maxCoinIn;
int[] coinList;

//constructor
public CoinSorter(String Currency, int minValueToExchange, int maxValueToExchange, int[] initialCoinList) {
    currency=Currency;
    minCoinIn=minValueToExchange;
    maxCoinIn = maxValueToExchange;
    coinList= initialCoinList;
    
}

            public void printCoinList() {
    System.out.println("The current coin denominations are in circulation : "
            + Arrays.toString(coinList));
}


public static void main(String[] args) {
    //An example
    CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, new int[]{10,20,50,100,200});
    exampleOne.printCoinList();
    
}

}

Result : The current coin denominations are in circulation : [10, 20, 50, 100, 200]

Good luck :) I hope I could access to you future exchange and buy and hold some crypto :D

Comments

0

before creating an object CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, {10,20,50,100,200}); declare and initialize an array int arr[]={10,20,50,100,200}and then pass it in the constructor rather than passing {10,20,50,100,200}

like this

int arr[]={10,20,50,100,200};
CoinSorter exampleOne = new CoinSorter("pounds", 0, 10000, arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.