Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,413 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,443 people online right now. Registration is fast and FREE... Join Now!




Need Help Finishing up School Assignment

 
Reply to this topicStart new topic

Need Help Finishing up School Assignment, ']' expecter error message

he4dhuntr
14 Oct, 2008 - 12:37 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

Hey guys,
I'm doing a school assignment for my Object Oriented Java programing class. Here's the assignment requirements:

"There are five kinds of flowers, and their associated costs. Create an array of strings that holds the names of these flowers. Create another array that holds the cost of each corresponding flower. Your program should read the name of a flower and the quantity desireed by a customer. Locate the flower in the name array and use that index to find the cost per stem in the cost array. Compute and print the total cost of the sale. Write your solution in one class: FlowerCounterArray.
The solution should implement the following two methods:\
- addOrder(String flowerName, int quantity) to add to the order. This is in addition to reading from the terminal.
- printTotal() to print out the total cost to the terminal."

I wrote up some code so far and I want to test it, but I am getting this error: "']' expected" on the newFlower[0] = new FlowerCounterArrays("petunia", 0.50); line near the end of the code.

Here's my code:
CODE

public class FlowerCounterArrays {

    private String flowerName;
    private double price;
    
    public void addOrder(String flowerName, int quantity) {
        for (FlowerCounterArrays flower : newFlower) {
            if (flower.equals(flowerName)) {
                total = price * quantity;
                System.out.println("The price for " + quantity + " " + flower + "(s) is " + total);
            }
            else {
                System.out.println("Unfortunately we do not carry that type of flower here.");
            }
        }
    }
    
    public void printTotal() {
        System.out.println(total);
    }
    
    FlowerCounterArrays[] newFlower = new FlowerCounterArrays[5];
    
    newFlower[0] = new FlowerCounterArrays("petunia", 0.50);
    newFlower[1] = new FlowerCounterArrays("pansy", 0.75);
    newFlower[2] = new FlowerCounterArrays("rose", 1.50);
    newFlower[3] = new FlowerCounterArrays("violet", 0.50);
    newFlower[4] = new FlowerCounterArrays("carnation", 0.80);
}


Thanks for the help guys! Much appreciated!

This post has been edited by he4dhuntr: 14 Oct, 2008 - 12:43 PM
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Need Help Finishing Up School Assignment
14 Oct, 2008 - 12:40 PM
Post #2

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
change this
CODE

newFlower[0] = new FlowerCounterArrays("petunia", 0.50];


to
CODE

newFlower[0] = new FlowerCounterArrays("petunia", 0.50);

you opened a parenthesis and closed it with a bracket, that doesn't work
User is online!Profile CardPM
+Quote Post

he4dhuntr
RE: Need Help Finishing Up School Assignment
14 Oct, 2008 - 12:44 PM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

Yea sorry, that was a typo when I pasted the code in here. The actual code has a ) at the end of it. Still getting the same error message though! Any other ideas? Thanks so far!
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Need Help Finishing Up School Assignment
14 Oct, 2008 - 12:57 PM
Post #4

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 164



Thanked: 1 times
My Contributions
QUOTE(he4dhuntr @ 14 Oct, 2008 - 01:44 PM) *

Yea sorry, that was a typo when I pasted the code in here. The actual code has a ) at the end of it. Still getting the same error message though! Any other ideas? Thanks so far!


Well, if you are getting the same error my guess is that you have a typo somewhere else in your program, somewhere you are missing a bracket, or parenthesis...
User is online!Profile CardPM
+Quote Post

he4dhuntr
RE: Need Help Finishing Up School Assignment
14 Oct, 2008 - 01:08 PM
Post #5

New D.I.C Head
*

Joined: 9 Oct, 2008
Posts: 20

I figured out the problem. I had the assignment outside of any method, just free floating in the body of the class. I fixed it by moving the code, but now I get a new error: "cannot find symbol - constructor FlowerCounterArrays(java.lang.String,double)"
I get this error on my newFlower[0] = new FlowerCounterArrays("petunia", 0.50); line

I'm kind of new to all this, so I'm not 100% on how everything should be done. I really appreciate the help thought! Here's my new code:

CODE

public class FlowerCounterArrays {

    private String flowerName;
    private double price;
    
    public void addOrder(String flowerName, int quantity) {
        FlowerCounterArrays[] newFlower = new FlowerCounterArrays[5];
    
        newFlower[0] = new FlowerCounterArrays("petunia", 0.50);
        newFlower[1] = new FlowerCounterArrays("pansy", 0.75);
        newFlower[2] = new FlowerCounterArrays("rose", 1.50);
        newFlower[3] = new FlowerCounterArrays("violet", 0.50);
        newFlower[4] = new FlowerCounterArrays("carnation", 0.80);
        
        for (FlowerCounterArrays flower : newFlower) {
            if (flower.equals(flowerName)) {
                total = price * quantity;
                System.out.println("The price for " + quantity + " " + flower + "(s) is " + total);
            }
            else {
                System.out.println("Unfortunately we do not carry that type of flower here.");
            }
        }
    }

    public void printTotal() {
        System.out.println(total);
    }
}


Thanks for the help so far!

This post has been edited by he4dhuntr: 14 Oct, 2008 - 01:09 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Need Help Finishing Up School Assignment
14 Oct, 2008 - 08:44 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
QUOTE(he4dhuntr @ 14 Oct, 2008 - 02:08 PM) *

I figured out the problem. I had the assignment outside of any method, just free floating in the body of the class. I fixed it by moving the code, but now I get a new error: "cannot find symbol - constructor FlowerCounterArrays(java.lang.String,double)"


As the error message says, your class FlowerCounterArrays does not have a constructor.

FlowerCountersArray has:
- a method named: addOrder
- a method named: printTotal

but no constructor and you call 5 times the constructor:

newFlower[0] = new FlowerCounterArrays("petunia", 0.50);
newFlower[1] = new FlowerCounterArrays("pansy", 0.75);
newFlower[2] = new FlowerCounterArrays("rose", 1.50);
newFlower[3] = new FlowerCounterArrays("violet", 0.50);
newFlower[4] = new FlowerCounterArrays("carnation", 0.80);

that expects a String as first parameter and a double as second parameter
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:37PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month