Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:50:46+00:00 2026-05-23T13:50:46+00:00

I am trying to write a program that uses file I/O to score personality

  • 0

I am trying to write a program that uses file I/O to score personality tests. However, when I get to this code:

while (f.hasNextLine()) {

I get the error described in the title. Here is the code in its entirety. I import all of the necessary I/O and utilities prior and I think I am declaring the file variable correctly. I know that this method exists, but what is the problem?

import java.io.*;
import java.util.*;
public class PersonalityTest {
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        String input = input();
        String output = output();
        results(input, output);
        System.out.println("Your results are located in the file you requested.");
    }

    public static String input() throws IOException {
        System.out.print("Input file name: ");
        String file = input.nextLine();
        File f = new File(file);
        System.out.println();

        while (!f.exists()) {
            System.out.print("File not found. Try again: ");
            file = input.nextLine();
            f = new File(file);
            System.out.println();
        }

        return file;
    }

    public static String output() {
        System.out.print("Output file name: ");
        String output = input.nextLine();
        return output;
    }

    public static void results(String input, String output) throws IOException {
        PrintStream write = new PrintStream(new File(output));
        File f = new File(input);

        while (f.hasNextLine()) {
            String name = f.nextLine();
            String type = "ESTJ";
            String answers = f.nextLine();
            char[] choices = new char[70];

            for (int i = 0; i <= 69; i++) {
                choices[i] = answers.charAt(i);
            }

            int aCount = 0;
            int bCount = 0;

            for (int i = 0; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount+=1;
                }
            }

            int pct1 = (int)Math.round((bCount/(aCount+bCount))*100);

            if (pct1 > 50) {
                type.replace('E','I');
            }
            if (pct1 == 50) {
                type.replace('E','X');
            }

            int aCount2 = 0;
            int bCount2 = 0;

            for (int i = 2; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount2+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount2+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount2+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount2+=1;
                }
            }

            int pct2 = (int)Math.round((bCount2/(aCount2+bCount2))*100);

            if (pct2 > 50) {
                type.replace('S','N');
            }
            if (pct2 == 50) {
                type.replace('S','X');
            }

            int aCount3 = 0;
            int bCount3 = 0;

            for (int i = 4; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount3+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount3+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount3+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount3+=1;
                }
            }

            int pct3 = (int)Math.round((bCount3/(aCount3+bCount3))*100);

            if (pct3 > 50) {
                type.replace('T','F');
            }
            if (pct3 == 50) {
                type.replace('T','X');
            }

            int aCount4 = 0;
            int bCount4 = 0;

            for (int i = 6; i <= 69; i+=7) {
                if (choices[i].toLowerCase == 'a') {
                    aCount4+=1;
                }
                if (choices[i].toLowerCase == 'b') {
                    bCount4+=1;
                }
                if (choices[i+1].toLowerCase == 'a') {
                    aCount4+=1;
                }
                if (choices[i+1].toLowerCase == 'b') {
                    bCount4+=1;
                }
            }

            int pct4 = (int)Math.round((bCount4/(aCount4+bCount4))*100);

            if (pct4 > 50) {
                type.replace('J','P');
            }
            if (pct4 == 50) {
                type.replace('J','X');
            }

            write.println(name + ": [" + pct1 + ", " + pct2 + ", " + pct3 + ", " + pct4 + "] = " + type);

            write.close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:50:47+00:00Added an answer on May 23, 2026 at 1:50 pm

    java.io.File does not have a hasNextLine() method. That’s a method that exists in java.util.Scanner. Scanner has a constructor that takes a File object as an argument and allows it to use the specified file for input – you should probably try using that one:

    Scanner s = new Scanner(new File(input));
    

    EDIT:

    That said, Scanner can be a bit of a performance killer (a bit?). It is often faster to use a BufferedReader and its readLine() method to read a single line into a String object and then parse the String manually. You can get a BufferedReader from a File with a bit of trickery:

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a program that uses a C API. This API
I'm trying to write a program that uses sockets to connect with other instances
I'm trying to write a program in Java that uses osql to generate a
I'm trying to write a program that reads text from external file (string string
I'm trying to write a program that takes a large file (of any type)
I'm trying to write a program that could monitor multiple folders for file creations
I am trying to write a program that uses a Berkeley Database and i
I'm trying to write a program with g++ that uses conio.h header. What I'm
So I'm trying to write a C program that uses inotify. I've used pyinotify
gud day... i'm trying to write a program in javascript that uses .json. i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.