0

input.txt入力行の各文字が小文字の場合は大文字に、大文字の場合は小文字に変わるように、各行を読み取って各行を印刷しようとしています。さらに、Thread各行の反転も印刷したいので、これを行うために a を使用したいと思います。

とのエラーが発生しprintUppLow uppLow = new printUppLow();ますprintRev rev = new printRev();

non-static variable this cannot be referenced from a static context.

コード

public static void main(String args[])
{
    String inputfileName="input.txt";     // A file with some text in it
    String outputfileName="output.txt";   // File created by this program
    String oneLine;

    try {
        // Open the input file
        FileReader fr = new FileReader(inputfileName);
        BufferedReader br = new BufferedReader(fr);

        // Create the output file
        FileWriter fw = new FileWriter(outputfileName);
        BufferedWriter bw = new BufferedWriter(fw);

        printRev rev = new printRev();
        printUppLow uppLow = new printUppLow();
        rev.start();
        uppLow.start();

        // Read the first line
        oneLine = br.readLine();
        while (oneLine != null) { // Until the line is not empty (will be when you reach End of file)

            // Print characters from input file
            System.out.println(oneLine);  

            bw.newLine();
            // Read next line
            oneLine = br.readLine(); 
        }

        // Close the streams
        br.close();
        bw.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}


public class printUppLow extends Thread 
{
    public void run(String str)
    {
        String result = ""; 
        for (char c : str.toCharArray()) 
        { 
            if (Character.isUpperCase(c)){ 
                result += Character.toLowerCase(c); // Convert uppercase to lowercase
            } 
            else{ 
                result += Character.toUpperCase(c); // Convert lowercase to uppercase
            } 
        } 
        return result;  // Return the result
    }
}

public class printRev extends Thread
{
    public void run()
    {
        StringBuffer a = new StringBuffer("input.txt");
        System.out.println(a.reverse());
    }
}
4

2 に答える 2

0

これは、ネストされたクラスprintUppLowprintRevが宣言されていないstaticため、インスタンス相対であるためです。

staticこれを解決するには、それらをメイン クラスから宣言するか、宣言にキーワードを追加して宣言します。

(ちなみに、クラス名に大文字を使用していないと、おそらく苦情を受けるでしょう。)

于 2014-10-21T04:01:21.713 に答える