Results 1 to 10 of 10
- 06-29-2011, 01:48 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
New to Java .....help please....be nice too!
Yes I posted earlier. Researched what I could find on "proper indenting" and couldn't find anything helpful! I receive one error in CoffeeDriver class. Item class compiles. The error I receive is in the creation of the Array. Item is defined in the Item class. I compile Item before trying to compile CoffeeDriver.
Error is: cannot find symbol
symbol : variable Item
location: class CoffeeDriver
Many thanks!
Java Code:import java.util.*; import java.util.ArrayList; import java.util.Comparator; public class CoffeeDriver { //main method public static void main (String[] args){ Scanner input = new Scanner(System.in); String decision; //Create the Array Item[] items = Item[5]; items[0]= new Item("Coffee",1.00); items[1]= new Item("Water",2.00); items[2]= new Item("Milk",1.50); items[3]= new Item("Donut",0.75); items[4]= new Item("Bagel",1.25); System.out.println ("Welcome to Wings Coffee Shop"); System.out.println ("We have a great list of tasty items on our menu."); System.out.println ("Would you like to see these items sorted by"); System.out.println ("name or by price? (n/p): "); decision = input.nextLine(); if (decision == "n"){ sortName(items); } else sortPrice(items); }//End Main //method to sort by item name and display public static void sortName (Item[]r){ for (int i = 0; i < r.length; i++){ System.out.println (r[i].toString()); } }//End Sort by itemName //method to sort by item price and display public static void sortPrice (Item[]r){ for (int i = 0; i < r.length; i++){ System.out.println (r[i]); } }//End Sort by itemPrice }//End Class
- 06-29-2011, 01:56 AM #2
Where is the definition for the Item class? What folder is it in? What folder is the CoffeeDriver class in?
The compiler can't find the definition for the Item class. Are any of the classes in a package?
Here is a suggested formatting for your code. Line up { or code with the { with the ending }.
Put all code in a pair of {} in column alignment.
Java Code://main method public static void main (String[] args){ Scanner input = new Scanner(System.in); String decision; //Create the Array Item[] items = Item[5]; items[0]= new Item("Coffee",1.00); items[1]= new Item("Water",2.00); items[2]= new Item("Milk",1.50); items[3]= new Item("Donut",0.75); items[4]= new Item("Bagel",1.25); System.out.println ("Welcome to Wings Coffee Shop"); System.out.println ("We have a great list of tasty items on our menu."); System.out.println ("Would you like to see these items sorted by"); System.out.println ("name or by price? (n/p): "); decision = input.nextLine(); if (decision == "n"){ sortName(items); } else sortPrice(items); }//End main()Last edited by Norm; 06-29-2011 at 02:00 AM.
- 06-29-2011, 02:22 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Hello
Item class and CoffeeDriver are both in the same folder of c:/java/. I have tried to verify that each { had a corresponding }. Is there a particular rule for what kind of statement gets a }? If so, where does it end with {? How do I tell if there is a package? I didn't code a package. Does that matter?
- 06-29-2011, 02:38 AM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
I reformatted your code to have indent according to convention. maybe comparing the two will help you understand. It may not seem important to you now, but the more you code, the more you will realize the importance of adhering to convention.
note: I tried to do this in the text box of the website so it may not be perfect
[/QUOTE]Java Code:import java.util.*; import java.util.ArrayList; import java.util.Comparator; public class CoffeeDriver { //main method public static void main (String[] args){ Scanner input = new Scanner(System.in); String decision; //Create the Array Item[] items = Item[5]; items[0]= new Item("Coffee",1.00); items[1]= new Item("Water",2.00); items[2]= new Item("Milk",1.50); items[3]= new Item("Donut",0.75); items[4]= new Item("Bagel",1.25); System.out.println ("Welcome to Wings Coffee Shop"); System.out.println ("We have a great list of tasty items on our menu."); System.out.println ("Would you like to see these items sorted by"); System.out.println ("name or by price? (n/p): "); decision = input.nextLine(); if (decision == "n"){ sortName(items); }else{ sortPrice(items); } }//End Main //method to sort by item name and display public static void sortName (Item[]r){ for (int i = 0; i < r.length; i++){ System.out.println (r[i].toString()); } }//End Sort by itemName //method to sort by item price and display public static void sortPrice (Item[]r){ for (int i = 0; i < r.length; i++){ System.out.println (r[i]); } }//End Sort by itemPrice }//End Class
- 06-29-2011, 02:43 AM #5
If both the Item.java and the CoffeeDriver.java files are in the same folder and neither has a package statement,
I don't know what to recommend. Do you have the Item.java file or just the Item.class file? If you don't have the Item.java file how do you know if the Item class is in a package or not?
- 06-29-2011, 03:50 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Item.java and CoffeeDriver.java are in the same folder. There is an Item.class file as well. The Item class looks like this:
Java Code:public class Item{ //A String instance variable for the item name private String itemName; //A double instance variable to hold the price private double itemPrice; //A constructor that takes a String and double to initialize the instance variables public Item (String n,double p){ itemPrice = p; itemName = n; } //Getters and setters methods for each instance variable public void setItemName(String n) { itemName=n; } public void setItemPrice(double p) { itemPrice=p; } public String getName() { return itemName; } public double getPrice() { return itemPrice; } }//end class
- 06-29-2011, 04:03 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Thank you very much YellowLedBet. That helped me organize it better. Still receive errors though.
- 06-29-2011, 04:11 AM #8
Please post the full text of the error message.Still receive errors though.
- 06-29-2011, 04:13 AM #9
The code with a pair of {} should be indented:
Java Code:public Item (String n,double p){ itemPrice = p; itemName = n; }
- 06-29-2011, 06:07 AM #10
Code Conventions for the Java(TM) Programming Language: ContentsResearched what I could find on "proper indenting" and couldn't find anything helpful!
db
Similar Threads
-
New look! Nice!
By mine0926 in forum Forum LobbyReplies: 6Last Post: 06-27-2011, 04:59 PM -
Nice GUI
By karq in forum New To JavaReplies: 13Last Post: 08-09-2010, 10:49 AM -
nice question
By pavankumar2215 in forum New To JavaReplies: 17Last Post: 06-27-2010, 03:46 AM -
Nice Forum
By VijayKumar23 in forum IntroductionsReplies: 0Last Post: 04-15-2009, 12:22 PM -
[SOLVED] Building a nice GUI applet?
By bobleny in forum New To JavaReplies: 3Last Post: 05-09-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks