Ask Experts Questions for FREE Help !
Ask
    Darkrune495's Avatar
    Darkrune495 Posts: 2, Reputation: 1
    New Member
     
    #1

    Feb 7, 2008, 11:05 AM
    Unknown Array Java Error
    I'm having a problem with some java code involving an array and I don't understand the error that appears:
    CD.java:67: array required, but CDDriver found
    System.out.println(myCD[i]);

    I am trying to print out the total of all the prices that are in the array

    Any help would be great

    here is the whole code

    class CD
    {
    String artist;
    String title;
    double cost;
    static double totalPrice;
    static int totalCDs;
    double total=0;

    CD (String a, String t, double c)
    {
    artist=a;
    title=t;
    cost=c;
    }

    public String toString()
    {
    return artist + "\t " + title + "\t " + cost;
    }

    public String getArtist()
    {
    return artist;
    }

    public String getTitle()
    {
    return title;
    }

    public double getCost()
    {
    return cost;
    }

    public static double getTotalPrice()
    {
    return totalPrice;
    }

    public int getTotalCDs()
    {
    return totalCDs;
    }

    public void setTotalPrice(double newTotalPrice)
    {
    totalPrice = newTotalPrice;
    }

    public double findTotalPrice(CDDriver myCD)
    {
    for (int I=0; I<3; I++)
    {
    System.out.println(myCD[i].getCost());
    }
    return total;
    }

    }
    -------------------------------------------------

    class CDDriver
    {
    public static void main(String args[])
    {
    CD myCD [] =new CD[3];


    myCD[0] = new CD("Killers\t","Hot Fuss",6.49);
    myCD[1] = new CD("Kaiser Chiefs", "Employment",6.49);
    myCD[2] = new CD("Leonard Cohen", "The Essential",7.99);

    System.out.println("Artist \t\tTitle\t\tCost");
    System.out.println("============================== ======");

    for(int I=0; I<myCD.length; I++)
    {
    System.out.println(myCD[i].toString());
    }

    int maxIndex=0;
    double maxCost=myCD[0].getCost();

    for(int I=1; I<myCD.length; I++)
    {
    if (myCD[i].getCost()>maxCost)
    {
    maxCost=myCD[i].getCost();
    maxIndex=i;
    }
    }

    printTotals(myCD, myCD.length);

    System.out.println("\nThe most expensive CD is in location "+maxIndex+" and is: \n"+ myCD[maxIndex].toString());

    System.out.println("\n" + "Total price of all CDs is " + CD.getTotalPrice());
    }

    public static void printTotals(CD[] myCD, int number)
    {
    System.out.println("Total Number of CDs \t Total Price");

    System.out.println(number + "\t\t" + myCD.findTotalPrice());
    }


    }
    Sitwonade's Avatar
    Sitwonade Posts: 24, Reputation: 3
    New Member
     
    #2

    Feb 29, 2008, 10:22 AM
    Where is the CDDriver class?

    Edit: Sorry, I must be blind.
    Darkrune495's Avatar
    Darkrune495 Posts: 2, Reputation: 1
    New Member
     
    #3

    Feb 29, 2008, 11:26 AM
    Underneath the CD class
    They are separate by a line of dashes
    ----------------------------------------------
    Sitwonade's Avatar
    Sitwonade Posts: 24, Reputation: 3
    New Member
     
    #4

    Feb 29, 2008, 12:00 PM
    In CDDriver:
    [CODE=Java]System.out.println("\n" + "Total price of all CDs is " + CD.getTotalPrice());[/CODE]
    CD.getTotalPrice() will return "0" unless you've previously done a CD.setTotalPrice().

    More importantly:
    [CODE=Java]System.out.println(number + "\t\t" + myCD.findTotalPrice());[/CODE]
    Does not match this from the CD class:
    [CODE=Java]public double findTotalPrice(CDDriver myCD)[/CODE]

    Also you can't do this:
    [CODE=Java]System.out.println(myCD[i].getCost());[/CODE]
    Because myCD is not an array here.


    Try this:
    [CODE=Java]class CD
    {
    String artist;
    String title;
    double cost;
    static double totalPrice;
    static int totalCDs;
    double total=0;

    CD (String a, String t, double c)
    {
    artist=a;
    title=t;
    cost=c;
    }

    public String toString()
    {
    return artist + "\t " + title + "\t " + cost;
    }

    public String getArtist()
    {
    return artist;
    }

    public String getTitle()
    {
    return title;
    }

    public double getCost()
    {
    return cost;
    }

    public static double getTotalPrice()
    {
    return totalPrice;
    }

    public int getTotalCDs()
    {
    return totalCDs;
    }

    public static void setTotalPrice(double newTotalPrice)
    {
    totalPrice = newTotalPrice;
    }

    public static void setTotalPrice(CD[] myCD)
    {
    double total = 0;
    for (int i=0; i<myCD.length; i++)
    {
    total += myCD[i].getCost();
    }
    totalPrice = total;
    }

    public static double getTotalPrice(CD[] myCD)
    {
    double total = 0;
    for (int i=0; i<myCD.length; i++)
    {
    total += myCD[i].getCost();
    }
    return total;
    }

    public static double findTotalPrice(CD[] myCD)
    {
    for (int i=0; i<myCD.length; i++)
    {
    System.out.println(myCD[i].getCost());
    }
    return getTotalPrice(myCD);
    }

    }
    [/CODE]
    And
    [CODE=Java]class CDDriver
    {
    public static void main(String args[])
    {
    CD myCD [] =new CD[3];


    myCD[0] = new CD("Killers\t","Hot Fuss",6.49);
    myCD[1] = new CD("Kaiser Chiefs", "Employment",6.49);
    myCD[2] = new CD("Leonard Cohen", "The Essential",7.99);

    System.out.println("Artist \t\tTitle\t\tCost");
    System.out.println("============================== ======");

    for(int i=0; i<myCD.length; i++)
    {
    System.out.println(myCD[i].toString());
    }

    int maxIndex=0;
    double maxCost=myCD[0].getCost();

    for(int i=1; i<myCD.length; i++)
    {
    if (myCD[i].getCost()>maxCost)
    {
    maxCost=myCD[i].getCost();
    maxIndex=i;
    }
    }

    printTotals(myCD, myCD.length);

    System.out.println("\nThe most expensive CD is in location "+maxIndex+" and is: \n"+ myCD[maxIndex].toString());

    System.out.println("\n" + "Total price of all CDs is " + CD.getTotalPrice());
    }

    public static void printTotals(CD[] myCD/*, int number <- not needed*/)
    {
    System.out.println("Total Number of CDs \t Total Price");

    System.out.println(myCD.length + "\t\t" + myCD.findTotalPrice(myCD));
    }


    }[/CODE]


    P.S.: And by the way, I like your taste in music. ;-)

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java Virtual Machine error [ 1 Answers ]

In trying to access my Yahoo Site Builder, the Java Virtual Machine Launcher window pops up and says "could not create the virtual java machine". How do I get anound or fix this problem ?

An array [ 1 Answers ]

How many ratings do you wish to enter? 10 Rating #1: 9 Rating #2: 5 Rating #3: 9 Rating #4: 2 Rating #5: 11 Invalid entry, score must be in range of 1 and 10; try again. Rating #5: -1 Invalid entry, score must be in range of 1 and 10; try again. Rating #5: 8

JNLPException[category: Download Error : Exception:java.net.ConnectException: Connect [ 0 Answers ]

Hi all , it's woking in server Because code base = cPanel... if am calling the same application in client/ end User .. Error Connection Refuse.. clear about the Error message: An error occurred while launching/running the application. Title: My Pirate! Vendor: me Category: Download Error

Java: using an array from the main in a method [ 2 Answers ]

I have the array names in the main of my program and I want to use it in a method FindName.. How would I wright the method header and the call to the method in the main?

Java Error: array required, but PRmList found [ 2 Answers ]

Here is my code: I get the error: Array required, but PRmList found. Any help would be GREATLY appreciated. Thanks! Public class PRm { Private String PRm; Private String movieTitle; Private String movieRating; Private int seats;


View more questions Search