无法在控制台上打印字符串

时间:2015-11-17 15:44:04

标签: java printf

我无法弄清楚为什么我在编译这个简单的程序时会在控制台中出错,它应该只打印一个字符串(2个单独的文件):

/*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

这些是我得到的错误:

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

我只是创建一个对象,构造函数将字符串赋给变量并打印出该变量。我仍然不太明白为什么这不是编译 感谢

2 个答案:

答案 0 :(得分:0)

System.out.printf("%s", getString());导致错误,因为它不在示例中的方法或构造函数中。您可以将它放在构造函数中,如果这是您希望它运行的地方:

public WordCount(){
    stringToTest = "This is the string I want to test, and I'm curious to see the results.";
    System.out.printf("%s", getString());
}

或用它自己的方法,例如:

public void printString() {
    System.out.printf("%s", getString());
}

答案 1 :(得分:0)

我怀疑是因为你的print语句不包含在方法中。尝试类似下面的内容

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

然后你可以用以下方式使用它

public class WordCountTest{
    public static void main(String[] args){
         WordCount wordCount = new WordCount();     
         wordCount.printString(); 
    }
}//end of WordCountTest