"不能应用于给定的类型" PrintStream期间出错

时间:2013-11-21 02:39:07

标签: java compiler-errors printstream

我现在还不完全确定我的代码是怎么回事。我正试图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

最初,我遇到的原始问题是我的主要内容中没有两个同名的变量。所以我改变了它,并在我的其余代码中包含了参数。我做到了,并在我的主要内部称它。但是现在,我仍然遇到这个错误,我不完全确定它试图告诉我什么。

我该怎么办?

1 个答案:

答案 0 :(得分: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个参数