Errors in the java program

I am trying to write this java program to add, remove items in an array list. This is what I encoded:

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 get:

 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 made my mistake. thanks in advance

+4
source share
3 answers

Change this:

  public void static main(String args[]) 

to

  public static void main(String args[]) 

You also need import java.util.ArrayList; use lists. And your list cannot be of type <int> , it must be <Integer> . Int is a number; Integer is a pointer that can refer to an object containing a number. Read the difference here .

+5
source

You want the return type to be after the identifier:

 public static void main(String args[]) 

Also, you have generics for Int , do you want them to be Integer ? You add primitives to ArrayList.

+1
source

You use NetBeans if you are a student programmer. You must also use the return type for the main method, since it has a return type of void

0
source

All Articles