1

I'm not entirely sure what's going on with my code right now. I'm attempting to PrintStream it but I'm getting all sorts of errors regardless of what I do.

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

The error I keep getting is this:

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

Initially, the original problem I was getting was that I couldn't have two variables with the same name within my main. So I changed that and also included parameters for it throughout the rest of my code. I did that and also called it within my main. But now, I'm still getting this error and I'm not entirely sure what it's attempting to tell me.

What should I do?

cmd
  • 11,622
  • 7
  • 51
  • 61

1 Answers1

0

Error:

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

look carefully:

//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));

}

You dont have an overload of asksForFile() that takes 1 arguments

  • Oh you mean in my main? Or in the method "asksForFile"? – user3015607 Nov 21 '13 at 02:50
  • 1
    @user3015607 your function is like this `Scanner asksForFile(Scanner scanner, PrintStream output)` but you call it using only `asksForFile(input);` – WarehouseMuzik Nov 21 '13 at 02:53
  • I see, I changed it from asksForFile(input); to Scanner scanner = asksForFile(Scanner scanner, PrintStream output); in my main, and I end up getting the error Personality.java:16: error: ')' expected Scanner scanner = asksForFile(Scanner scanner, PrintStream output); ^ Personality.java:16: error: illegal start of expression Scanner scanner = asksForFile(Scanner scanner, PrintStream output); – user3015607 Nov 21 '13 at 02:56
  • 1
    it should be `Scanner scanner = asksForFile(input, output);` – WarehouseMuzik Nov 21 '13 at 02:58