簡體   English   中英

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

[英]Can't print a string on the console

我無法弄清楚為什么在編譯這個簡單程序時會在控制台中出現錯誤,該程序應該只打印一個字符串(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

我只是創建一個對象,構造函數將字符串分配給變量,然后打印出該變量。 還是我不太明白為什么這個不編譯謝謝

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());
}

我懷疑是因為您的打印語句不包含在方法中。 試試下面的東西

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM