Call Me Dummy
Call Me Dummy

Reputation: 55

Errors in java program

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

Upvotes: 4

Views: 245

Answers (3)

0x6C38
0x6C38

Reputation: 7076

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.

Upvotes: 5

Tech Nerd
Tech Nerd

Reputation: 832

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

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143886

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.

Upvotes: 1

Related Questions