• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invalid method declaration

 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i compiled my code i get 2 errors can someone assit me in fixing these ..

C:\jdk1.6.0\bin>javac InventoryProgram.java
InventoryProgram.java:18: ';' expected
public String to String(){
^
InventoryProgram.java:18: invalid method declaration; return type required
public String to String(){
^
2 errors

My Code is

public class CD{
public String name;
public int itemNumber;
public int stockQuantity;
public double price;
public CD(String name, int itemNumber, int stockQuantity, double price){
this.name = name;
this.itemNumber = itemNumber;
this.stockQuantity = stockQuantity;
this.price = price;
}

public String to String(){
return "CD Title: " + this.name + "\n" +
"Item Number: " + this.itemNumber + "\n" +
"Stock Quantity: " + this.stockQuantity + "\n" +
"Price: " + this.price + "\n";
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public int getItemNumber(){
return this.itemNumber;
}

public void setItemNumber(int itemNumber){
this.itemNumber = itemNumber;
}

public int getStockQuantity(){
return this.stockQuantity;
}

public void setStockQuantity(int stockQuantity){
this.stockQuantity = stockQuantity;
}

public double getPrice(){
return this.price;
}

public void setPrice(double price){
this.price = price;

} //end method main
} //end class InventoryProgram
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aretha Clark:
... public String to String(){...

You can't have a space in a method name. Try "toString" instead of "to String."
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A method name cannot have a space!
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you Guys

Now i have this error after i run the program

C:\jdk1.6.0\bin>java InventoryProgram
Exception in thread "main" java.lang.NoSuchMethodError: main

public class InventoryProgram{
public String name;
public int itemNumber;
public int stockQuantity;
public double price;
public InventoryProgram(String name, int itemNumber, int stockQuantity, double price){
this.name = name;
this.itemNumber = itemNumber;
this.stockQuantity = stockQuantity;
this.price = price;
}

public String toString(){
return "CD Title: " + this.name + "\n" +
"Item Number: " + this.itemNumber + "\n" +
"Stock Quantity: " + this.stockQuantity + "\n" +
"Price: " + this.price + "\n";
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public int getItemNumber(){
return this.itemNumber;
}

public void setItemNumber(int itemNumber){
this.itemNumber = itemNumber;
}

public int getStockQuantity(){
return this.stockQuantity;
}

public void setStockQuantity(int stockQuantity){
this.stockQuantity = stockQuantity;
}

public double getPrice(){
return this.price;
}

public void setPrice(double price){
this.price = price;

} //end method main
} //end class InventoryProgram
[ March 24, 2008: Message edited by: Aretha Clark ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i may just not be seeing it, but where IS your main() method?
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tryed adding one and still somehow manage to get that error.. So i must be having a lack of brain function moment.. Any help with this will be much appreciated
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, show us your code with the main method.

A common mistake is to get the signature wrong. Make sure that it's exactly

public static void main(String[] args)
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HERE IS THE CODE
C:\jdk1.5.0_14\bin>javac InventoryProgram.java
InventoryProgram.java:10: ';' expected
^
1 error


public class InventoryProgram{

public static void main(String[] args)

public String name;
public int itemNumber;
public int stockQuantity;
public double price;
public InventoryProgram(String name, int itemNumber, int stockQuantity, double price){
this.name = name;
this.itemNumber = itemNumber;
this.stockQuantity = stockQuantity;
this.price = price;
}

public String toString(){
return "CD Title: " + this.name + "\n" +
"Item Number: " + this.itemNumber + "\n" +
"Stock Quantity: " + this.stockQuantity + "\n" +
"Price: " + this.price + "\n";
}

public String getName(){
return this.name;
}

public void setName(String name){
this.name = name;
}

public int getItemNumber(){
return this.itemNumber;
}

public void setItemNumber(int itemNumber){
this.itemNumber = itemNumber;
}

public int getStockQuantity(){
return this.stockQuantity;
}

public void setStockQuantity(int stockQuantity){
this.stockQuantity = stockQuantity;
}

public double getPrice(){
return this.price;
}

public void setPrice(double price){
this.price = price;

} //end method main
} //end class InventoryProgram
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method is the starting point for your program. It is not just something you put in to get your program to magically compile.

If you want your class to compile, then you need to add the braces, like so...


public static void main(String[] args) {
// You actually have to put something here to make your
// program do something !!!
}


If you want your program to actually do something, you need to write the code for it.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic