I can't figure out why I'm getting errors in the console when I compile this simple program, which should just print a string (2 separate files):
/*WordCount.java*/
//for the map
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeSet;
public class WordCount{
private String stringToTest;
//Map<String,Integer> mp = new HashMap<String, Integer>();//declaring a map object
//constructor
public WordCount(){
stringToTest = "This is the string I want to test, and I'm curious to see the results.";
}//end of constructor
public String getString(){
return stringToTest;
}
System.out.printf("%s ", getString());
}//end of WordCount class
/*WordCountTest.java
WordCountTest to test the class
*/
public class WordCountTest{
public static void main(String[] args){
WordCount wordCount = new WordCount();
}
}//end of WordCountTest
These are the errors I get:
G:\JAVA\GUI\2015\createFrames\word_counter\test>javac *.java
WordCount.java:19: error: <identifier> expected
System.out.printf("%s", getString());
^
WordCount.java:19: error: illegal start of type
System.out.printf("%s", getString());
^
WordCount.java:19: error: <identifier> expected
System.out.printf("%s", getString());
^
WordCount.java:19: error: ';' expected
System.out.printf("%s", getString());
^
WordCount.java:19: error: illegal start of type
System.out.printf("%s", getString());
^
WordCount.java:19: error: <identifier> expected
System.out.printf("%s", getString());
^
WordCount.java:19: error: ';' expected
System.out.printf("%s", getString());
^
WordCount.java:21: error: reached end of file while parsing
}//end of WordCount class
^
8 errors
I'm just creating an object, the constructor assign the string to the variable and I print out that variable. Still I can't quite understand why this isn't compiling Thanks
the line System.out.printf("%s", getString());
is causing an error because it is not in a method or constructor in your example. You can either put it in the constructor, if that is where you want it to be run:
public WordCount(){
stringToTest = "This is the string I want to test, and I'm curious to see the results.";
System.out.printf("%s", getString());
}
or in it's own method, eg:
public void printString() {
System.out.printf("%s", getString());
}
I suspect it's because your print statement isn't contained within a method. Try something like the following
public class WordCount{
private String stringToTest;
//Map<String,Integer> mp = new HashMap<String, Integer>();//declaring a map object
//constructor
public WordCount(){
stringToTest = "This is the string I want to test, and I'm curious to see the results.";
}//end of constructor
public String getString(){
return stringToTest;
}
public void printString() {
System.out.printf("%s ", getString());
}
}//end of WordCount class
And then you can use it in the following way
public class WordCountTest{
public static void main(String[] args){
WordCount wordCount = new WordCount();
wordCount.printString();
}
}//end of WordCountTest
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.