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

    Default ArrayList Errors

    Hello, I am working on a project that is two programs. One is named Item and it compiles just fine. The other is called CoffeeDriver. The class named CoffeeDriver. CoffeeDriver errors on the ArrayList line and all following in that line. My code is below and the errors are too.

    CoffeeDriver.java:51: class, interface, or enum expected
    ArrayList<Item> item = new ArrayList<Item>();
    ^
    CoffeeDriver.java:52: class, interface, or enum expected
    item.coffee new item("Coffee",1.0);


    import java.util.*;
    import java.util.ArrayList;
    import java.util.Comparator;

    public class CoffeeDriver {

    //Main method
    public static void main (String[] args){
    String[] itemName = {"Coffee, Water, Milk, Donut, Bagel"};
    double[] itemPrice = {1.00, 2.00, 1.50, 0.75, 1.25};
    Scanner input = new Scanner(System.in);
    String decision;

    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(itemName);
    }
    else
    sortPrice(itemPrice);

    }//End Main

    //method to sort by item name and display
    public static void sortName (String[] array){
    for (int i = 0; i < array.length; i++){
    System.out.println (array[i].toString());
    }
    }//End Sort by itemName

    //method to sort by item price and display
    public static void sortPrice (double[] array){
    for (int i = 0; i < array.length; i++){
    System.out.println (array[i]);
    }
    }//End Sort by itemPrice
    }//End Class

    ArrayList<Item> item = new ArrayList<Item>();
    item.coffee new item("Coffee",1.0);
    item.water new item("Water",2.0);
    item.milk new item("Milk",1.5);
    item.donut new item("Donut",.75);
    item.bagel new item("Bagel",1.2);

    java.util.Collections.sort;item,new Comparator<Item>()

    public int compare(Item a,Item b)

    if(a.getName().compareTo(b.getName())>0){
    return 1;
    else
    if(a.getName().compareTo(b.getName())<0)
    return -1;
    else
    return 0;
    }
    public boolean equals(Object a)

    return false;

    });


    Thank you for your help.

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

    Default

    It think that The code with the error is not inside of a method.

    Please wrap your code in code tags. Your posted code is hard to read without formatting.

    Use the # icon above to right or see: BB Code List - Java Forums

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

    Default

    It think that The code with the error is not inside of a method.

    Please wrap your code in code tags. Your posted code is hard to read without formatting.

    Use the # icon above to right or see: BB Code List - Java Forums

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

    Default

    Please explain code tags. New to java.

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

    Default

    Did you look at the link at the end of my post?

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

    Default

    Java Code:
    CoffeeDriver.java:51: class, interface, or enum expected
    ArrayList<Item> item = new ArrayList<Item>();
    ^
    CoffeeDriver.java:52: class, interface, or enum expected
    item.coffee new item("Coffee",1.0);
    
    
    import java.util.*;
    import java.util.ArrayList;
    import java.util.Comparator;
    
    public class CoffeeDriver {
    
    //Main method
    public static void main (String[] args){
    String[] itemName = {"Coffee, Water, Milk, Donut, Bagel"};
    double[] itemPrice = {1.00, 2.00, 1.50, 0.75, 1.25};
    Scanner input = new Scanner(System.in);
    String decision;
    
    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(itemName);
    }
    else 
    sortPrice(itemPrice);
    
    }//End Main
    
    //method to sort by item name and display
    public static void sortName (String[] array){
    for (int i = 0; i < array.length; i++){
    System.out.println (array[i].toString());
    }
    }//End Sort by itemName
    
    //method to sort by item price and display
    public static void sortPrice (double[] array){
    for (int i = 0; i < array.length; i++){
    System.out.println (array[i]);
    }
    }//End Sort by itemPrice
    }//End Class
    
    ArrayList<Item> item = new ArrayList<Item>();
    item.coffee new item("Coffee",1.0);
    item.water new item("Water",2.0);
    item.milk new item("Milk",1.5);
    item.donut new item("Donut",.75);
    item.bagel new item("Bagel",1.2);
    
    java.util.Collections.sort;item,new Comparator<Item>()
    
    public int compare(Item a,Item b)
    
    if(a.getName().compareTo(b.getName())>0){
    return 1;
    else
    if(a.getName().compareTo(b.getName())<0)
    return -1;
    else
    return 0; 
    }
    public boolean equals(Object a)
    
    return false;
    
    });
    Better?

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

    Default

    Is your code really all left adjusted like it shows in your post?
    The first thing you need to do is edit your code and indent it to show nesting levels within {}. That will make it a lot easier to see your problem.

    Also you don't need to put the error message text in the code tags

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

    Default

    Not it isn't. I will re-upload appropriately.

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

    Default

    Java Code:
    import java.util.*;
    import java.util.ArrayList;
    import java.util.Comparator;
    
    public class CoffeeDriver {
    
    			//main method
        		public static void main (String[] args){
          	String[] itemName = {"Coffee, Water, Milk, Donut, Bagel"};
             double[] itemPrice = {1.00, 2.00, 1.50, 0.75, 1.25};
             Scanner input = new Scanner(System.in);
             String decision;
    
             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(itemName);
                    }
                else 
                    sortPrice(itemPrice);
                    
        	}//End Main
    
    		//method to sort by item name and display
        	  public static void sortName (String[] array){
            for (int i = 0; i < array.length; i++){
                System.out.println (array[i].toString());
                }
       	 }//End Sort by itemName
    
    		//method to sort by item price and display
        	public static void sortPrice (double[] array){
            for (int i = 0; i < array.length; i++){
                System.out.println (array[i]);
                }
        }//End Sort by itemPrice
    }//End Class
    
     ArrayList<Item> item = new ArrayList<Item>();
       item.coffee new item("Coffee",1.0);
       item.water new item("Water",2.0);
       item.milk  new item("Milk",1.5);
       item.donut new item("Donut",.75);
       item.bagel new item("Bagel",1.2);
    	
      java.util.Collections.sort;item,new Comparator<Item>()
      
       public int compare(Item a,Item b)
       
        if(a.getName().compareTo(b.getName())>0){
               return 1;
             else
             if(a.getName().compareTo(b.getName())<0)
             return -1;
             else
             return 0;            
            }
           public boolean equals(Object a)
           
           return false;
           
        });

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    No.
    You need to format your code.
    Indent it correctly, and you'll probably see straight away where the problem is.

    ETA: Oops. I must have left that window open a little too loong!
    :)

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
    }//End Class
    
     ArrayList<Item> item = new ArrayList<Item>();
    You can't have code outside of a class.

    ETA: And you do really need to indent your code properly.

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

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Arraylist errors with private fields
    By DJRome in forum New To Java
    Replies: 5
    Last Post: 03-02-2010, 12:00 PM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •