File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Help with program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with program" Watch "Help with program" New topic

Help with program

sandy skiathitis

Joined: Apr 22, 2011
Posts: 18
Ok so i need to make a program that allows a user to input what items they want from a menu that is displayed on screen. After their input, the user should get back a total price for their order. Now, I want a second class that shows the owner how much he made in one day, and also the number of items he sold. this is what I got so far. I am so lost first time ever programmed

........................this is my second class

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Welcome to the Ranch

I am afraid I can see some errors; for example item.indexof() won't work because item is an array and arrays don't have an indexof method. And $4.55 for onion rings? Nobody will ever eat there at those prices

Joking apart, you are going to have to start small. Very small. don't write more than about 5 lines without compiling the code and running it. Start with a Meal class, which can have two fields, the description and the price. Give it the standard methods of the Object class (well, at least toString() just for now), and set up a main method which creates a couple of Meal objects and prints them out. You will also have to give your class a constructor. There is a bit in the Java™ Tutorials about that. Remember to give the two fields private access.Now you can set up an array of Meals, and give each a number (remember arrays start with no 0). You can enter a number via your Scanner, and add the price to the total price.
Your for loop won't work, for two reasons:
  • You have declared i twice.
  • You are testing whether i < 0, and that loop will never actually start.

  • Anyway, try some of those stages, and see how far you get.

    Write down on a piece of paper what you think your methods ought to do. Write a few lines and get them working before you think of writing any more. Good luck, and tell us how you get on.
    sandy skiathitis

    Joined: Apr 22, 2011
    Posts: 18
    ok so I did what you said, but i am still a little lost. The word Meal after the equals sign does not work, why?? Also, once I fix that how can I calculate the customers total? should i use a for loop or indexof method?? or what...... can you show me an example. Thanks!

    package final2;

    * @author kyriakhskiathitis

    import java.util.Scanner;
    public class Main {

    public static void main(String[] args) {
    String Mealm1 = new Meal("Peperoni Pizza", 9.99);
    String Mealm2 = new Meal("Onion Rings", 4.55);
    String Mealm3 = new Meal("Plain Pizza", 7.99);
    String Mealm4 = new Meal("Plain Steak", 5.20);
    String Mealm5 = new Meal("Cheese Steak", 6.00);
    String Mealm6 = new Meal("French Fries", 2.25);
    String Mealm7 = new Meal("Mozzarella Sticks", 4.79);
    String Mealm8 = new Meal("20 ounces Bottled Soda-Any", 1.85);
    String Mealm9 = new Meal("Cheese Cake plain", 2.50);
    String Mealm10 = new Meal("Cherry Cheese Cake", 2.50);


    Scanner keyboard=new Scanner(;

    System.out.println("Please enter your items");
    String answer=keyboard.nextLine();

    int sum=0;
    int i=0;
    for( i=0; i<sum; i++)
    System.out.println("You have entered nothing.");

    //calculate the order total.




    Vijitha Kumara

    Joined: Mar 24, 2008
    Posts: 3902

    Hi Sandy,
    Please UseCodeTags while posting code . (your original post has been edited by Campbell, I guess it's for code tags).

    You are trying too many things here in one class. First try to implement a small part as already pointed out. Don't try everything at once it'll cause more problem than it solves.

    Some points for you:
    - Have a separate class which wraps the menu to be displayed to the user.
    - You should not loop through the "sum", instead what the user entered. For example he may allow
    to enter his preferred item as a comma separated string so you can extract them in your program.

    SCJP 5 | SCWCD 5
    [How to ask questions] [Twitter]
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46362
    sandy skiathitis wrote:o . . . The word Meal after the equals sign does not work, why??
    It's not an equals sign, but an assignment operator. You are supposed to write new Meal()
    Also, once I fix that how can I calculate the customers total? should i use a for loop or indexof method??
    I would prefer a loop; you can put in some nonsense value like -1 to terminate the loop
    or what...... can you show me an example.
    I have already shown you how to create new Meal objects. I don't think I ought to show you any more at this stage.
    I think you are going to have to put your Meal objects into an arrayNow you can loop through the array and print them out with numbers like a Chinese restaurant and you can order a No 3. Remember the first element of an array is No 0.
    Thanks! . . .
    You're welcome.

    Beware of names of packages like "final2"; since final is a keyword, you can get some strange errors like that.
    I agree. Here's the link:
    subject: Help with program
    It's not a secret anymore!