I’m sure this has been discussed prior but I am having issues testing my code working on the Language Families project I worked on it yesterday and am starting over today and am getting these errors:
“ expected
‘;’ expected
reached end of file while parsing (aka missing closing or opening curly brace ({})”
Yet I checked and I end every necessary line with a semicolon and pair each curly brace so I don’t know what I’m doing wrong. I tried adding “public” before class but it doesn’t matter because I still get the same errors. Here’s my code:
class Language{
protected String name;
protected int numSpeakers;
protected String regionsSpoken;
protected String wordOrder;
class Language(String langName, int numOfSpeakers, String regions, String wdOrder)
{
this.name = langName;
this.numSpeakers = numOfSpeakers;
this.regionsSpoken = regions;
this.wordOrder = wdOrder;
}
public void getInfo(){
System.out.println(langName + " is spoken by " + numOfSpeakers + " people mainly in " + regions + "." + "\n" + "The languagefollows the word order: " + wdOrder);
}
public static void main(String[] args)
{
Language spanish = new Spanish("Spanish", 8000000, "Latin America, South America, Spain, Central America and parts of the Carribean", "subject, verb then, object");
spanish.getInfo();
}
}
Specifically, a function cannot return a type class (in c-style languages).
Writing class Foo(int bar){} as a function signature implies that one is return a type class.
You can think of a constructor as a special method that doesn’t have a return type.
All other non-constructor methods must have a return type before the function name.
And for the curious, if you had an object of type Language you could have a function Language foo() that returns an object of type Language.
Thanks for catching the last line before <-spanigh.getInfo();-> I stupidly forgot to have spanish be a new Language instead of new Spanish. facepalm Now I still get errors after adding public and removing class from the Constructor line.
[codebyte language=java]
public class Language{
protected String name;
protected int numSpeakers;
protected String regionsSpoken;
protected String wordOrder;
public Language(String langName, int numOfSpeakers, String regions, String wdOrder)
{
this.name = langName;
this.numSpeakers = numOfSpeakers;
this.regionsSpoken = regions;
this.wordOrder = wdOrder;
}
public void getInfo(){
System.out.println(langName + " is spoken by " + numOfSpeakers + " people mainly in " + regions + “.” + “\n” + "The languagefollows the word order: " + wdOrder);
}
public static void main(String args)
{
Language spanish = new Language(“Spanish”, 8000000, “Latin America, South America, Spain, Central America and parts of the Carribean”, “subject, verb then, object”);
spanish.getInfo();
}
Error message is saying it doesn’t recognize variables “regions”, “numOfSpeakers”,and “wdOrder”? Even though I declared and initialzed them in the contructor method?`
Has to do with how this works.
When you set this.something to a value, that instance of the object’s something will now be that value.
So for example
Foo something1 = new Foo("the thing");
Foo something2 = new Foo("the other thing");
Foo might have a construct where
this.thing = newThing;
And in these two instances were setting this.thing to “the thing” and “the other thing”.
in your code these variables langName, wdOrder, numOfSpeakers etc have no place as data members to your object.
public void getInfo(){
System.out.println(langName + " is spoken by " + numOfSpeakers + " people mainly in " + regions + “.” + “\n” + "The languagefollows the word order: " + wdOrder);
}
should replace the data members with the ones defined with this. I think it suffices to just use name, numSpeakers, regionsSpoken, and wordOrder.