0

I'm currently attempting to make my code produce an output file by using the PrintStream method. MY textbook suggests that I use this particular line of code within my main:

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

However, when I type up this line of code, Java gives me the following error:

    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();
              ^

My main method currently looks like this:

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

I'm guessing from the error, that I can't have output defined twice in a single method, but if I don't define my output, how will my program know what that symbol is? Also, if I already have output define within main, what else can I call it to make the PrintStream work, while also keeping the rest of the program running as well?

I renamed the variables of "output" in my main to scanner, but I'm getting this new error instead:

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

This is what my entire code looks like at the moment:

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

}

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • Care: Java is *not* related to JavaScript. – user2864740 Nov 21 '13 at 02:12
  • Sorry about that! I'm still getting errors in my code! Do you have any idea why? – user3015607 Nov 21 '13 at 02:33
  • Pay close attention to the error messages - they are correct. The currently posted form doesn't work because `PrintStream output` is a *local variable* in the `main` method and thus [the symbol which was meant to refer to a variable] *cannot be found* in the `intro` method. – user2864740 Nov 21 '13 at 02:42
  • (I would make `intro` *take* a PrintStream object as an argument.) – user2864740 Nov 21 '13 at 02:44

1 Answers1

0

The code has two variables with the same name, which is not allowed. Change one of these variable names and refactor the code:

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

REFACTORED VERSION

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

NOTE There are several methods in this code snippet for which the source is not provided. This version may need additional refactoring given these unknowns.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I refactored the code like you said, but I'm still getting errors! I edited the main question to show what errors I'm getting. – user3015607 Nov 21 '13 at 01:56