Ask Experts Questions for FREE Help!
  Advanced
Register  |  Log in  
   Ask    
 Answer  
  Help  

Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
Free Answers in 3 Easy Steps

Register Now
3 Steps

At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.

Home > Computers & Technology > Programming > Compiled Languages > Java   »   Unknown Array Java Error

 
Thread Tools Display Modes
Question
 
 
#1  
Old Feb 7, 2008, 09:05 AM
Darkrune495
New Member
Darkrune495 is offline
 
Join Date: Feb 2008
Posts: 2
Darkrune495 See this member's comment history on his/her Profile page.
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());
}


}

Reply With Quote
 
     

Answers
 
 
Old Feb 29, 2008, 08:22 AM   #2  
New Member
Sitwonade is offline
 
Join Date: Feb 2008
Posts: 24
Sitwonade See this member's comment history on his/her Profile page.
Where is the CDDriver class?

Edit: Sorry, I must be blind.
  Reply With Quote
 
     
 
 
Old Feb 29, 2008, 09:26 AM   #3  
New Member
Darkrune495 is offline
 
Join Date: Feb 2008
Posts: 2
Darkrune495 See this member's comment history on his/her Profile page.
underneath the CD class
they are separate by a line of dashes
----------------------------------------------
  Reply With Quote
 
     
 
 
Old Feb 29, 2008, 10:00 AM   #4  
New Member
Sitwonade is offline
 
Join Date: Feb 2008
Posts: 24
Sitwonade See this member's comment history on his/her Profile page.
in CDDriver:
Java Code:
System.out.println("\n" + "Total price of all CDs is " + CD.getTotalPrice());
CD.getTotalPrice() will return "0" unless you've previously done a CD.setTotalPrice().

More importantly:
Java Code:
System.out.println(number + "\t\t" + myCD.findTotalPrice());
does not match this from the CD class:
Java Code:
public double findTotalPrice(CDDriver myCD)

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


try this:
Java 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 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);
    }

}
and
Java Code:
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));
    }


}


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


Thread Tools
Display Modes

 
Similar Sponsors

Similar Threads
Question Asker Forum Answers Last Post
Java Virtual Machine error jonmcfaddin Apple & Macintosh 1 Jan 17, 2008 05:24 AM
an array Crispy C++ 1 Oct 15, 2007 09:12 AM
JNLPException[category: Download Error : Exception:java.net.ConnectException: Connect dhana_msc Windows 0 Apr 27, 2006 09:30 PM
Java: using an array from the main in a method kilroy Internet & the Web 2 Jul 30, 2003 11:37 AM
Java Error: array required, but PRmList found brooksn Internet & the Web 2 Mar 17, 2003 02:07 PM




Copyright ©2003 - 2007, Ask Me Help Desk.
All times are GMT -8. The time now is 02:58 AM.