0

現在、PrintStream メソッドを使用してコードで出力ファイルを生成しようとしています。私の教科書では、メイン内でこの特定のコード行を使用することを提案しています。

    PrintStream output = new PrintStream(new File("results.txt"));

ただし、このコード行を入力すると、Java で次のエラーが表示されます。

    Personality.java:17: error: variable output is already defined in method main(String[])
    PrintStream output = new PrintStream(new File("results.txt"));
                ^
    Personality.java:23: error: cannot find symbol
        output.println();
              ^

現在、私の主な方法は次のようになっています。

    public class Personality {
public static void main (String[] args)  throws FileNotFoundException   {
    Scanner input = new Scanner(System.in);
    intro();
    Scanner output = asksForFile(input);
    PrintStream output = new PrintStream(new File("results.txt"));
  while(output.hasNextLine()){
        int[] aCounts = new int[4];
        int[] bCounts = new int[4];
        String name = output.nextLine();
        String data = output.nextLine();
        output.println();
        System.out.print(name + ": ");
        int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
        output.print(Arrays.toString(percentB));
     output.print(" = ");
        output.println(determineType(percentB));    
    }       
}

エラーから、1 つのメソッドで出力を 2 回定義することはできないと推測していますが、出力を定義しない場合、プログラムはそのシンボルが何であるかをどのように認識しますか? また、main 内にすでに出力定義がある場合、PrintStream を機能させるために他に何を呼び出し、プログラムの残りの部分も同様に実行し続けることができますか?

メインの「出力」の変数の名前をスキャナーに変更しましたが、代わりに次の新しいエラーが発生しています。

    Personality.java:34: error: cannot find symbol
    output.println("This program processes a file of answers to the");
    ^
      symbol:   variable output
  location: class Personality

これは、現時点で私のコード全体がどのように見えるかです:

import java.util.*;
import java.io.*;

public class Personality {
    public static void main (String[] args)  throws FileNotFoundException   {
        Scanner input = new Scanner(System.in);
        intro();
        Scanner scanner = asksForFile(input);
        PrintStream output = new PrintStream(new File("results.txt"));

      while(scanner.hasNextLine()){
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            String name = scanner.nextLine();
            String data = scanner.nextLine();
            output.println();
            System.out.print(name + ": ");
            int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
            output.print(Arrays.toString(percentB));
         output.print(" = ");
            output.println(determineType(percentB));    
        }       
    }

   //Introduces the program
    public static void intro()  {
        output.println("This program processes a file of answers to the");
        output.println("Keirsey Temperament Sorter.  It converts the");
      output.println("various A and B answers for each person into");
      output.println("a sequence of B-percentages and then into a");
        output.println("four-letter personality type.");
        output.println();       
    }

    //Asks for input file
    public static Scanner asksForFile(Scanner input) throws FileNotFoundException   {
        output.print("input file name? ");
        String filename = input.nextLine();
        return new Scanner(new File(filename));

    }

   //This while loop puts counts inside arrays
    public static int[] numberOfAnswers(String name, String data, int[] aCounts, int[] bCounts)  throws FileNotFoundException   {
        data = data.toLowerCase();
        for (int i = 0; i < data.length(); i++) {
            int x = ((i % 7) + 1) / 2;
            if (data.charAt(i) == 'a'){
                aCounts[x]++;
            } else if(data.charAt(i) == 'b'){
                bCounts[x]++;
            }
        }
        return percentB(aCounts, bCounts);
    }

    public static void printOutcome(int[] aCounts, int[] bCounts){
        String[] ratios = new String[4];
        for(int i = 0; i < 4; i++){
            String temp = aCounts[i] + "A-" + bCounts[i] + "B";
            ratios[i] = temp;
        }
        output.println(Arrays.toString(ratios));
    }

    public static int[] percentB(int[] aCounts, int[] bCounts){
        int[] percentB = new int[4];
        for(int i = 0; i < 4; i++){
            double percent = (double) bCounts[i] / (aCounts[i] + bCounts[i]);
            percentB[i] = (int) Math.round(percent * 100);
        }
        return percentB;    
    }

    public static String determineType(int[] percentB){
        String sub50 = "ESTJ";
        String sup50 = "INFP";
        String type = "";
        for(int i = 0; i < 4; i++){
            if(percentB[i] > 50){
                type += sup50.charAt(i);
            } else if(percentB[i] < 50){
                type += sub50.charAt(i);
            } else {
                type += "X";
            }
        }
        return type;
    }

}

4

1 に答える 1

0

コードには、許可されていない同じ名前の 2 つの変数があります。これらの変数名のいずれかを変更し、コードをリファクタリングします。

Scanner output = asksForFile(input);
PrintStream output = new PrintStream(new File("results.txt"));

リファクタリングされたバージョン

public class Personality {
    public static void main (String[] args)  throws FileNotFoundException   {
        Scanner input = new Scanner(System.in);
        intro();
        Scanner scanner = asksForFile(input);
        PrintStream output = new PrintStream(new File("results.txt"));

        while(scanner.hasNextLine()){
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            String name = scanner.nextLine();
            String data = scanner.nextLine();
            output.println();
            System.out.print(name + ": ");
            int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
            output.print(Arrays.toString(percentB));
            output.print(" = ");
            output.println(determineType(percentB));    
        }       
    }
}

このコード スニペットには、ソースが提供されていないメソッドがいくつかあります。このバージョンでは、これらの未知数を考慮して、追加のリファクタリングが必要になる場合があります。

于 2013-11-21T01:30:00.757 に答える