S.wong S.wong - 2 months ago 29
Java Question

Cannot instantiate the type in java

I was trying to run this code:

public class inventory {
private static item[] inventory;
static java.util.Scanner scanner = new java.util.Scanner(System.in);

private static int noOfItems;


public static void main(String[] args) {
noOfItems=0;

// TODO Auto-generated method stub
inventory=new item[10];
run();
}

public static int displayMenu()
{
System.out.println("1.Add Item");
System.out.println("2.Find Item");
System.out.println("3.Delete Item");
System.out.println("4.Count Item");
System.out.println("5.Exit");
System.out.println("Please enter your choice:");
int i=scanner.nextInt();
return i;
}

public static void run()
{
while(true)
{
int i=displayMenu();
switch(i)
{
case 1:addItem();
break;
case 2:findItem();
break;
case 4:countItem();
break;
case 5:return;
default:System.out.println("Invalid choice");
}
}
}

public static <Item> void addItem()
{
System.out.print("Enter Item namme:");
String item_name=scanner.next();
System.out.print("Enter barcode:");
String barcode=scanner.next();
System.out.print("Enter price:");
double price=scanner.nextDouble();
Item b=new Item(item_name,barcode,price);
if(noOfItems==inventory.length)
System.out.println("Array is full");
else
{
inventory[noOfItems++]=b;
System.out.println("Item added successfully");
}
}

public static void findItem()
{
System.out.print("Enter item name:");
String item_name=scanner.next();
for(int i=0; i<noOfItems; i++)
{
if(item_name.equalsIgnoreCase(inventory[i].getItem_name()))
{
System.out.println("Item found:");
System.out.print(inventory[i]);
return;
}
}

}

public static void countItems()
{
System.out.println("Num of items:"+noOfItems);
for(int i=0; i< noOfItems; i++)
System.out.println(inventory[i]);
}


}

Then, I got the error in "Item b=new Item(item_name,barcode,price);" about "Cannot instantiate the type in java"

public class item {
private String item_name;
private String barcode;
private double price;

//To initialise the state of the object
public void Item(String item_name,String barcode,double price)
{
this.item_name=item_name;
this.barcode=barcode;
this.price=price;
}

//Reader methods i.e behavior methods
public String getItem_name()
{
return item_name;
}

public String getBarcode()
{
return barcode;
}

public double getPrice()
{
return price;
}

//Writer methods or setter methods
public void setTitle(String item_name)
{
this.item_name=item_name;
}

public void setPrice(double price)
{
if(price < 0)
System.out.println("Price cannot be negative");
else
this.price=price;
}

public void setBarcode(String barcode)
{
this.barcode=barcode;
}
public String toString()
{
return "Item name:"+item_name+",Barcode:"+barcode+",Price:"+price;
}


}

And this is another code that I refer to the first one, actually the program didn't run properly too. Are there anyone can help me and fix the problem? Thanks.

Answer

There are several issues in your code:

1. The Constructor return type

A constructor has no return type so simply remove void in public void Item(String item_name,String barcode,double price) otherwise it won't be seen as constructor but as a normal method.

public Item(String item_name,String barcode,double price)
{
    this.item_name=item_name;
    this.barcode=barcode;
    this.price=price;
}

2. The Class name

Java is a case sensitive language such that you need to use the exact same name (case included) between your constructor and your class name so here if your constructor is public Item(String item_name,String barcode,double price) your class name should be Item not item. You will need to fix the class name everywhere in your code otherwise it won't work.

3. The bounded type of addItem

You need to remove <Item> from the method declaration of addItem() otherwise the compiler will be confused about the bounded type Item such that it won't compile.

So it should be:

public static void addItem()
Comments