4

I am trying to write this java program to add , delete items in array-list. This is what I have coded:

import java.io.*;
import java.util.Scanner;

class abc
{
    ArrayList<Int> nums = new ArrayList<Int>();
    Scanner in = new Scanner(System.in);
    int opt = 0; 

    public void addItem(int i) // add item to list
    {
        nums.add(i);
    }

    public void addItem(int i, int pos) // add item to specific position in list 
    {
        nums.add(pos,i);
    }

    public void delItem(int pos) // delete item at specific position in list 
    {
        nums.remove(pos);
    }   

    public void delItem() // clear all items in list 
    {
        nums.clear();
    }       

    public void showItems()
    {
        for(int i = 0; i < nums.length ; i++)
        {
            System.out.println("nums[" + i + "] : " + nums[i]);
        }
    }

    public void menu()
    {
        System.out.println("==MENU==");
        System.out.println("1) Add an item");
        System.out.println("2) Add an item to specific position");
        System.out.println("3) Delete an item at specific position");
        System.out.println("4) Clear all items in list");
        System.out.println("5) Exit \n\n");
        System.out.println("Choose an option");
        opt = in.nextInt();
        execute();
    }

    public void execute()
    {
        if(opt == 1)
        {
            System.out.println("Enter a value: ");
            int a = in.nextInt();
            addItem(a);
            System.out.println("Item added");
        }
        else if(opt == 2)
        {
            System.out.println("Enter a value: ");
            int a = in.nextInt();
            System.out.println("Enter a position: ");
            int b = in.nextInt();
            addItem(a,b);
            System.out.println("Item added");
        }
        else if(opt == 3)
        {
            System.out.println("Enter a position: ");
            int a = in.nextInt();
            delItem(a);
            System.out.println("Item deleted");
        }
        else if(opt == 4)
        {
            delItem();
            System.out.println("All Items deleted");
        }
    }
}


class pList
{
    public void static main(String args[])
    {
        abc a = new abc();
        while(true)
        {
            a.menu();
            if(a.opt == 5)
            {
                break;
            }
            else if(a.opt > 5)
            {
                a.menu();
            }
        }
    }
}

This is the error I am receiving:

C:\Users\Dummy\Desktop\Java>javac pList.java
pList.java:87: error: <identifier> expected
        public void static main(String args[])
                   ^
pList.java:87: error: '(' expected
        public void static main(String args[])
                    ^
pList.java:87: error: invalid method declaration; return type required
        public void static main(String args[])
                           ^
3 errors

C:\Users\Dummy\Desktop\Java>

Please let me know how I can fix this and where I have made my mistake. Thanks in advance

7
  • 1
    static comes before void Commented Apr 23, 2013 at 20:31
  • 1
    Please show the error text here, not in a link or image. You are asking for free advice and thus should put in the effort to make it as easy as possible for others to help you. Commented Apr 23, 2013 at 20:31
  • 2
    I recommend you start using an IDE, e.g. Eclipse Commented Apr 23, 2013 at 20:31
  • @HovercraftFullOfEels cmd is not allowing me to copy paste the text and if I type it I might inadvertently make some mistake. To be on the safe side I posted a screenshot Commented Apr 23, 2013 at 20:35
  • 1
    @CallMeDummy: cmd will let you copy text easily. Simply right click on the cmd window, click select all, and then press enter, and bingo, all of the text is copied. Next time you run into a similar problem, please do this and then post the error here, as it will simplify our work greatly and will be greatly appreciated by all. Commented Apr 23, 2013 at 20:37

3 Answers 3

5

Change this:

  public void static main(String args[])

to

  public static void main(String args[])

You also need to import java.util.ArrayList; in order to use lists. And your list cannot be of type <int>, has to be <Integer>. An int is a number; an Integer is a pointer that can reference an object that contains a number. Read the difference here.

Sign up to request clarification or add additional context in comments.

3 Comments

I fixed my code like u showed. But now I am receiving 4 new errors.C:\Users\Dummy\Desktop\Java>javac pList.java pList.java:6: error: cannot find symbol ArrayList<Int> nums = new ArrayList<Int>(); ^ symbol: class ArrayList location: class abc pList.java:6: error: cannot find symbol ArrayList<Int> nums = new ArrayList<Int>(); ^ symbol: class Int location: class abc
the error is too long to fit in one comment so the rest of it is here : pList.java:6: error: cannot find symbol ArrayList<Int> nums = new ArrayList<Int>(); ^ symbol: class ArrayList location: class abc pList.java:6: error: cannot find symbol ArrayList<Int> nums = new ArrayList<Int>(); ^ symbol: class Int location: class abc 4 errors
Thank you very much the program runs , I just need to fix the nums.length and nums[i] to nums.size() and nums.get(i) in my for loop
1

You want your return type to be after the identifier:

public static void main(String args[])

Additionally, you have generics for Int, do you want those to be an Integer? You're adding primitives into an ArrayList.

1 Comment

okay, I made them ArrayList<Integer> nums = new ArrayList<Integer>(); The number of error reduced to 2. ` C:\Users\Dummy\Desktop\Java>javac pList.java pList.java:6: error: cannot find symbol ArrayList<Integer> nums = new ArrayList<Integer>(); ^ symbol: class ArrayList location: class abc pList.java:6: error: cannot find symbol ArrayList<Integer> nums = new ArrayList<Integer>(); ^ symbol: class ArrayList location: class abc 2 errors C:\Users\Dummy\Desktop\Java>`
0

You shoul use NetBeans if you are a learner programmer You must use the return type for the main method too because it has a return type set to void

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.