Results 1 to 5 of 5
  1. #1
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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!

  4. #4
    johnb082 is offline Member
    Join Date
    Jun 2011
    Posts
    16
    Rep Power
    0

    Default

    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.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    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

  1. Struggling with enum!
    By XmisterIS in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 12:23 PM
  2. Friday night java puzzle
    By niu_niu in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 10:54 PM
  3. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 05:11 AM
  4. how to pretty print xml into file
    By BigBear in forum XML
    Replies: 2
    Last Post: 02-23-2010, 03:59 PM
  5. pretty simple array question
    By 711groove in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •