wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes invalid method declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "invalid method declaration" Watch "invalid method declaration" New topic
Author

invalid method declaration

Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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

Joined: Aug 31, 2004
Posts: 11343

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."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683

A method name cannot have a space!


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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

Joined: Oct 02, 2003
Posts: 11168
    
  16

i may just not be seeing it, but where IS your main() method?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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

Joined: Jul 11, 2001
Posts: 14112
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)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invalid method declaration
 
Similar Threads
Sorting Arrays
How to return class instance?
Need help with Car Inventory Program part 3
My loop isn't working
Why is my type unexpected? required variable found value