Results 1 to 12 of 12
Thread: ArrayList Errors
- 06-28-2011, 03:20 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
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.
- 06-28-2011, 04:19 PM #2
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
- 06-28-2011, 04:20 PM #3
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
- 06-28-2011, 04:41 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Please explain code tags. New to java.
- 06-28-2011, 04:42 PM #5
Did you look at the link at the end of my post?
- 06-28-2011, 04:46 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Better?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; });
- 06-28-2011, 04:49 PM #7
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
- 06-28-2011, 04:51 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Not it isn't. I will re-upload appropriately.
- 06-28-2011, 04:52 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
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; });
- 06-28-2011, 04:52 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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!
:)
- 06-28-2011, 04:54 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You can't have code outside of a class.Java Code:}//End Class ArrayList<Item> item = new ArrayList<Item>();
ETA: And you do really need to indent your code properly.
- 06-28-2011, 04:59 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
First Java Program-Compile Errors (errors are posted)-simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01-04-2011, 12:36 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Arraylist errors with private fields
By DJRome in forum New To JavaReplies: 5Last Post: 03-02-2010, 12:00 PM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks