1

我不完全确定我的代码现在发生了什么。我正在尝试,PrintStream但无论我做什么,我都会遇到各种各样的错误。

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

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

      intro(output);


      while(scanner.hasNextLine()){
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            String name = scanner.nextLine();
            String data = scanner.nextLine();
            output.println();
            output.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(PrintStream output)    {
        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 scanner, PrintStream output) 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, PrintStream output)  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;
    }
}

我不断收到的错误是:

Personality.java:16: error: method asksForFile in class Personality cannot be applied to given types;
      Scanner scanner = asksForFile(input);
                        ^
  required: Scanner,PrintStream
  found: Scanner
  reason: actual and formal argument lists differ in length

最初,我遇到的最初问题是我的 main.xml 文件中不能有两个同名的变量。所以我改变了它,并在我的其余代码中包含了它的参数。我这样做了,并且在我的主目录中也调用了它。但现在,我仍然收到这个错误,我不完全确定它试图告诉我什么。

我应该怎么办?

4

1 回答 1

0

错误:

Personality.java:16: error: method asksForFile in class Personality cannot be applied to given types;
      Scanner scanner = asksForFile(input);
                        ^
  required: Scanner,PrintStream
  found: Scanner
  reason: actual and formal argument lists differ in length

仔细看:

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

}

您没有asksForFile()需要 1 个参数的重载

于 2013-11-21T02:46:39.813 回答