JasonBreen
JasonBreen

Reputation: 31

Java expects a semicolon, when there is clearly one

public class Machine{
   String machineType;
   public Machine(String s){ //Sets machineType to String s
      String getName();
   }
}

Why wont this compile? I keep getting a error, saying that its expecting a semicolon, but there is one!

Upvotes: 0

Views: 9567

Answers (4)

Bohemian
Bohemian

Reputation: 425308

This line:

 String getName();

Makes no sense. Either change it to this:

 String name = getName();

Or more likely you want:

public Machine(String s) {
    machineType = s;
}

But I don't know how you could intend that but get the code you got.

Upvotes: 3

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

We cant call methods like this

 public Machine(String s){ //Sets machineType to String s
  String getName();    // Method
 }

Instead, assign it to a variable

 public Machine(String s){ //Sets machineType to String s
  String var = getName();    // assuming getName returns a string
 }

Even this would work

 public Machine(String s){ //Sets machineType to String s
   getName();    
 }

Upvotes: 3

Naveen
Naveen

Reputation: 367

Even you can do like this,

 public void Machine(String s){ 
      getName();
}

Upvotes: 0

Jesper Fyhr Knudsen
Jesper Fyhr Knudsen

Reputation: 7937

It seems like you are trying to call the method getName() If that is the case you either have to do:

getName();

or

String name = getName();

Use the first if the method has no return type (void), and the second if it is a String.

Upvotes: 0

Related Questions