Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Grocery List

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Grocery List

    I have many errors and issues with this. I am new to java and this code is throwing me way off. This is very important, please help.


     
     
    import java.util.*;
     
     
     
     
     
     
    public class Grocery {
      public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
     
      }
       private Scanner keybd;
        private ArrayList <> myList; //the <> are getting an error
     
      {
     
            keybd = new ArrayList(); //this is another error
            myList = new ArrayList(); //this is another error
        }
     
        public void startProgram()
        {
            int option = 0;
            System.out.println("Welcome to the Grocery Program!");
            System.out.println("1. Create a new Grocery List");
            System.out.println("2. Add sample items to the List");
            System.out.println("3. Remove an Item from the List");
            System.out.println("4. Print the list");
            System.out.println("5. Print the cost of all the Items");
            System.out.println("6. End the program");
            option = keybd.nextInt();
            keybd.nextLine();
     
             switch (option) {
     
     
                case 1:  createList(); //error
                //this is a break it will exit the loop
                break;
                case 2:  setList(); //error
                break;
                case 3:  removeItem(); //errpr
                break;
                case 4:  printList(); //error
                break;
                case 5:  printCost(); //error
                break;
                case 6:  endProgram(); //error
                break;
                default: inCorect(); //error
                startProgram();
                break;
            } 
        }
     
          private void createList()
        {
            //this is a stopper the program will continue until it is typed
            String stop = "st0p";
            String addAnother = ""; 
            String newItem = "";
            double newPrice = 0.0;
            //first we ask the question that the user will answer
            System.out.print("please enter an items name: ");
           String newItem = console.next();
            //next we need to check that variable with the stop
            while(!(newItem.equals(stop))){
                //now we ask the next question 
                System.out.print("please enter an items price: ");
                String newPrice = console.next();
                keybd.nextLine();
                //this will add a blank line
                System.out.println(); 
                //we than have to ask rhe question again 
                System.out.print("please enter an items name: ");
                String addAnother = console.next();
                if(newItem && newPrice ){
                    System.out.println("incorect item please try again");
                    createList();
                }else{
                    myList.add();
                }
            }
            startProgram();
        }
     
      /*  private void setList()
        {
            //this is sample data that we will fill in for testing purposes 
            myList.add();
            myList.add();
            myList.add();
            myList.add(new Grocery();
            myList.add(;
            myList.add();
            myList.add();
            startProgram();
        }
    */
        private void removeItem()
        {
            //this is a for statement you need to have three statements inside  
            for(){
                //i add a number in front of every item for easy access to data
                System.out.print(index + " " );
                myList.get(index).printItem();
            }
            //ask a question
            System.out.println("which item would you like to remove?");
             String remove = keybd.nextInt();
            myList.remove();
            startProgram();
        }
        /**
         * this will print the list
         */
        private void printList()
        {
            for(; ;index++){ 
                myList.get();
            }
            startProgram();
        }
        /**
         * print just the price
         */
        private void printCost()
        {
            for(; index < myList.size();){ 
                System.out.println(myList.get());
            }
            startProgram();
        }
        /**
         * this is an incorrect message 
         */
        private void inCorect()
        {
            System.out.println("the item you are looking for is invalid please try again");
        }
        /**
         * this is the end message 
         */
        private void endProgram()
        {
            System.out.println("Thanks for using this program");
        }
     
     
     
     
     
        private double price;
        private String name;
     
        /**
         * This is the constructor
         */
        public Grocery(String newName, double newPrice)
        { 
            name = newName;
            price = newPrice;
        }
     
     
        //======================+Accessors+=================================
        /**
         * this method is to return the name of the item
         * 
         * @return name of item 
         */
     
        public String getName()
        {
            return name;
        }
     
        /**
         * this will return the price
         * 
         * @return price of item 
         */
     
        public double getPrice()
        {
            return price;
        }
     
        //======================+Mutators+==================================
        /**
         * this will set a new name of an item
         * 
         * @param  newName a caller will set the name
         */
     
        public void setName(String newName)
        { 
            name = newName;
        }
     
        /**
         * this will set a new price of an item
         * 
         * @param  newPrice a caller will set the price
         */
     
        public void setPrice(double newPrice)
        { 
            price = newPrice;
        }
     
        /**
         * this will print an item
         */
     
        public void printItem()
        {
            System.out.println("the item is: " + name + " the price is: "+ price);
     
     
     
     
     
        }}


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Grocery List

    Please post the full text of any error message with your code and question.
    What is it you need help with?

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Grocery List

    Quote Originally Posted by jps View Post
    Please post the full text of any error message with your code and question.
    What is it you need help with?
    I just have like 16 errors and I have no clue where to start. Would you be able to test it and see the errors? Because it would take me forever to post the errors

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Grocery List

    Start with the first one and fix it. Then recompile and see what is first in the list again.
    Post the full text of the error message for better help.
    ArrayList should help with the errors at the top of the code

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Grocery List

    Quote Originally Posted by jps View Post
    Start with the first one and fix it. Then recompile and see what is first in the list again.
    Post the full text of the error message for better help.
    ArrayList should help with the errors at the top of the code
    Yeah, thats what I tried but I'm at a standstill

    18 errors and 2 warnings found:
    --------------
    *** Errors ***
    --------------
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 14]
    Error: Syntax error on token "<", ? expected after this token
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 18]
    Error: Type mismatch: cannot convert from java.util.ArrayList to java.util.Scanner
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 19]
    Error: myList cannot be resolved to a variable
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 41]
    Error: The method setList() is undefined for the type Grocery
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 66]
    Error: Duplicate local variable newItem
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 66]
    Error: console cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 71]
    Error: Duplicate local variable newPrice
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 71]
    Error: console cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 77]
    Error: Duplicate local variable addAnother
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 77]
    Error: console cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 78]
    Error: The operator && is undefined for the argument type(s) java.lang.String, java.lang.String
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 82]
    Error: myList cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 104]
    Error: Syntax error, insert "; ; ) Statement" to complete BlockStatements
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 120]
    Error: index cannot be resolved to a variable
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 121]
    Error: myList cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 130]
    Error: index cannot be resolved to a variable
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 130]
    Error: myList cannot be resolved
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 131]
    Error: myList cannot be resolved
    --------------
    ** Warnings **
    --------------
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 18]
    Warning: java.util.ArrayList is a raw type. References to generic type java.util.ArrayList<E> should be parameterized
    File: C:\Users\drzewiecki\Desktop\COSC 236\Grocery.java [line: 19]
    Warning: java.util.ArrayList is a raw type. References to generic type java.util.ArrayList<E> should be parameterized

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Grocery List

    Step back through the program and look at the brackets to start off with. I see a bunch of brackets that shouldn't be where they are.

    Also, the <> identifiers want a class name between them, not something blank. In this case I would assume it would be whatever type you had planned on storing inside the list.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Grocery List

    If you have not corrected the problem with the ArrayList, consult the link in post #4 and see what you can come up with.
    If you have, then post the revised code.
    According to the posted error message, you have not yet corrected the arraylists. Post any question you have in correcting them.

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM