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

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    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.

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

    Default

    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?

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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

    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
    [/QUOTE]

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    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?

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

    Default

    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

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

    Default

    Thank you very much YellowLedBet. That helped me organize it better. Still receive errors though.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Still receive errors though.
    Please post the full text of the error message.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    The code with a pair of {} should be indented:
    Java Code:
                              public Item (String n,double p){
    				   itemPrice = p; 
    				   itemName = n;
    			  }

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

Similar Threads

  1. New look! Nice!
    By mine0926 in forum Forum Lobby
    Replies: 6
    Last Post: 06-27-2011, 04:59 PM
  2. Nice GUI
    By karq in forum New To Java
    Replies: 13
    Last Post: 08-09-2010, 10:49 AM
  3. nice question
    By pavankumar2215 in forum New To Java
    Replies: 17
    Last Post: 06-27-2010, 03:46 AM
  4. Nice Forum
    By VijayKumar23 in forum Introductions
    Replies: 0
    Last Post: 04-15-2009, 12:22 PM
  5. [SOLVED] Building a nice GUI applet?
    By bobleny in forum New To Java
    Replies: 3
    Last Post: 05-09-2008, 07:42 PM

Posting Permissions

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