Results 1 to 5 of 5
- 06-29-2011, 07:14 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Still struggling today. Long night...help me pretty please
Hello again,
Many revisions have been made to my Item class and my CoffeeDriver class in an attempt to finish my assignment, late at that. Going gray! lol My code for item is first and it compiles fine. Code for CoffeeDriver is not compiling follows: Now Receiving:
CoffeeDriver.java:19: cannot find symbol
symbol : class NameComparator
location: class CoffeeDriver
Collections.sort(list,new NameComparator());
Java Code:import java.util.*; import java.util.ArrayList; import java.util.Comparator; import java.lang.*; 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 itemName, double itemPrice){ this.itemPrice = itemPrice; this.itemName = itemName;} public Item() { } //Getters and setters methods for each instance variable public String getName() { return itemName; } public double getPrice() { return itemPrice; } public void setItemName(String someItem) { itemName = someItem; } public void setItemPrice(double somePrice) { itemPrice = somePrice; } class NameComparator implements Comparator{ public int compare(Object o1, Object o2){ String name1 = ((Item)o1).getName(); String name2 = ((Item)o2).getName(); return name1.compareTo(name2); } } class PriceComparator implements Comparator{ public int compare(Object o1, Object o2){ if (((Item)o1).getPrice() < ((Item)o2).getPrice()) return -1; if (((Item)o1).getPrice() > ((Item)o2).getPrice()) return 1; return 0; } } } public class CoffeeDriver { //sortName Array static ArrayList<Item> list=new ArrayList<Item>(); public static void sortName(){ Collections.sort(list,new NameComparator()); for(Item data: list){ System.out.println(data.getName()+"\t "+data.getPrice()); } } //sortPriceArray public static void sortPrice(){ Collections.sort(list,new PriceComparator()); for(Item data: list){ System.out.println(data.getName()+"\t "+data.getPrice()); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); list.add(new Item("Bagel",1.25)); list.add(new Item("Water",2.00)); list.add(new Item("Coffee",1.00)); list.add(new Item("Donut",0.75)); list.add(new Item("Milk",1.50)); System.out.println("Sorted By Name"); System.out.println("Sorted By Price"); System.out.println("Exit"); boolean exit=false; do{ System.out.print("Enter your choice: "); int choice=input.nextInt(); switch(choice){ case 1: sortName(); break; case 2: sortPrice(); break; case 3: exit=true; break; default: System.out.println("Invalid"); } } while(!exit); } }//End Class
- 06-29-2011, 07:30 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
As far as I can see, your NameComparator class is an inner class of your Item class so it doesn´t exist by itself. b.t.w. your indentation stinks imho.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-29-2011, 07:30 PM #3
You can't access inner classes from other inner classes like that. Split this up into multiple class files.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 06-29-2011, 07:44 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Sorry but I'm so new to java I do not understand what you mean? CoffeeDriver is different than item class. Yes the NameComparator class is within the Item class. Is that what you mean? Please explain, not getting it. Overwhelmed and cannot wait to finish this class altogether. Programming is not my forte. Struggled all the way through while trying to maintain a 4.0. Having problems with that too. By the way, my indentation looks nothing like it shows here, just saying. Thanks though.
- 06-29-2011, 07:54 PM #5
Have you tried splitting this up into multiple class files?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Struggling with enum!
By XmisterIS in forum New To JavaReplies: 4Last Post: 09-03-2010, 12:23 PM -
Friday night java puzzle
By niu_niu in forum New To JavaReplies: 12Last Post: 06-19-2010, 10:54 PM -
Struggling with AlphaComposite .. please help me
By jeshmal4u in forum Java 2DReplies: 3Last Post: 03-08-2010, 05:11 AM -
how to pretty print xml into file
By BigBear in forum XMLReplies: 2Last Post: 02-23-2010, 03:59 PM -
pretty simple array question
By 711groove in forum New To JavaReplies: 8Last Post: 12-06-2009, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks