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

  • SEARCH
  • Home
  • 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 8924077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:18:37+00:00 2026-06-15T07:18:37+00:00

I had a quick questions about prompting and accepting a file name, then making

  • 0

I had a quick questions about prompting and accepting a file name, then making the file-text a scanner object.

I want the program to prompt the user to enter the name of a file, until he gets one which exists, then for the file-text to be used as a scanner object.

This is the code I have so far, it works to the point where I exit the while {} loop, but then when I try and process the scanner item like while (input.hasNextLine()) { it gives me an error saying it can’t find the scanner item.

It’s probably a silly mistake, but I just cannot seem to get it.

The whole code is below:

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

public class PersonalityTest {

public static void main(String[] args) throws FileNotFoundException {

    boolean isFile = false;

        Scanner sc = new Scanner(System.in);
        System.out.print("Input file name? ");
        String fileName = sc.next();
        File inputFile = new File(fileName);

    while (isFile == false) {    
        if (inputFile.exists()) {
            Scanner input = new Scanner(inputFile);
            isFile = true;
        }            
    }

    while(input.hasNextLine()) {

    }
}
  • 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-06-15T07:18:41+00:00Added an answer on June 15, 2026 at 7:18 am

    You can’t access input outside the if statement, sice the compiler is not sure, it will pass the test, you can do this:

    Scanner sc = new Scanner(System.in);
    Scanner input = null;
    boolean isFile = false;
    
    while (isFile == false){    
    
        System.out.print("Input file name? ");
        String fileName = sc.next();
        File inputFile = new File(fileName);
    
        if (inputFile.exists()){
            input = new Scanner(inputFile);
            isFile = true;
        }            
    }
    

    But will throw a NullPointerException if it is null.

    I changed the code a little bit, that way, it will not exceed if the file doesn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently making apps for iOS, and I had a quick question about making
I had a quick question about Django URL configuration, and I guess REGEX as
I just had a quick question about git - how backwards compatible are git
I am new to D3 and just had a quick question about tick labels
I had a quick question. Is it possible to save a file without actually
Had a quick question about Sphinx Search installation. Do I need to install it
Folks, I had a quick question about Amazon EC2, I have been recently using
Had a quick question about joins in mysql and indexing. If I had 2
Evening all, I just had a quick question about SQL average queries. I have
Quick question about JSON in general, say I had: var foo = { a:

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.