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 8885009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:12:13+00:00 2026-06-14T21:12:13+00:00

I need help figuring out how to loop my IOException (in where I ask

  • 0

I need help figuring out how to loop my IOException (in where I ask for a filename until a valid one is entered). I need a loop that somehow recognizes that an invalid file was entered and am unsure how to do this.

import java.util.*;
import java.io.*;
public class JavaGradedLab {
    public static void main(String[] args) throws IOException {

        Scanner inScan, fScan = null;
        int [] A = new int[5];
        inScan = new Scanner(System.in);
        System.out.println("Please enter the file to read from: ");

        try{
            String fName = inScan.nextLine();
            fScan = new Scanner(new File(fName));
        }

        catch (FileNotFoundException ex)
        {
            System.out.println("Your file is invalid -- please re-enter");
        }

        String nextItem;
        int nextInt = 0;
        int i = 0;

        while (fScan.hasNextLine())
        {
            nextItem = fScan.nextLine();
            nextInt = Integer.parseInt(nextItem);
            A[i] = nextInt;
            i++;
        }

        System.out.println("Here are your " + i + " items:");
        for (int j = 0; j < i; j++)
        {
            System.out.println(A[j] + " ");
        }
    }
}
  • 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-14T21:12:16+00:00Added an answer on June 14, 2026 at 9:12 pm

    Well, there’s sure to be someone explaining how to make your code better via best practices etc., but as a very basic answer which in itself can probably be improved (assuming that your code works when the input is valid):

    while(true) {
        try{
            String fName = inScan.nextLine();
            fScan = new Scanner(new File(fName));
            break;
        }
        catch (FileNotFoundException ex)
        {
            System.out.println("Your file is invalid -- please re-enter");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help figuring out to intakes a complete date and then validate that
I need help figuring out how to create a cell like the one representing
I need help figuring out how to convert data that comes in from a
I need some help figuring out this for loop logic. I know exactly what
I need help figuring out bits of code. I need a function that loads
I need some help figuring out the SQL for combining two tables into one
I need help figuring out one issue in my script, options ( c, e
I have weird problem with django translations that i need help figuring out. Problem
I need help figuring out why my rspec tests are running slowly. I am
I need some help figuring out how I should do this. I would like

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.