This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

invalid method declaration

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A method name cannot have a space!
 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
lowercase baba
Posts: 13025
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic