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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:19:19+00:00 2026-06-10T23:19:19+00:00

In my program, it reads a file called datafile.txt… inside the datafile.txt is a

  • 0

In my program, it reads a file called datafile.txt… inside the datafile.txt is a random 3 lines of words. What my program does is reads the file the user types in and then they can type in a Line # and Word # and it will tell them the word that is in that location.. for example..

What is the file to read from?

datafile.txt

Please enter the line number and word number (the first line is 1).

2 2

The word is: the

My problem is that my program reads the 3 lines in the txt doc as 0, 1 ,2 and the words start from 0. So to read the first word in the first line they would have to type 0,0 instead of 1,1. What I am trying to do is make it work so they can type 1,1 instead of 0,0. Not sure what my problem is right now, here is my code….

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class readingFile {

    /**
    * @param args
    * @throws IOException 
    * @throws validException 
    */
    public static void main(String[] args) throws IOException, checkException 
    {
        System.out.println("Enter file name: " );
        Scanner keyboard = new Scanner(System.in);
        BufferedReader inputStream = null;
        ArrayList<String> file = new ArrayList<String>();
        String fileName = keyboard.next();
        System.out.println ("The file " + fileName +
                " has the following lines below: ");
        System.out.println();

        try
        {
            inputStream = new BufferedReader(new FileReader(fileName));         
            ArrayList<String> lines = new ArrayList<String>();
            while(true)
            {
                String line = inputStream.readLine();
                if(line ==null)
                {
                    break;
                }

                Scanner itemnize = new Scanner(line);
                while(itemnize.hasNext())
                {
                    lines.add(itemnize.next());
                }
                lines.addAll(lines);

                System.out.println(lines+"\n");                 
            }       

            System.out.println("Please enter the line number and word number");

            int index1 = keyboard.nextInt();
            int index = keyboard.nextInt();

            System.out.println("The word is: "+ lines.get(index));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error opening the file " + fileName);
        }
        inputStream.close();
    }

    private static void checkValid(ArrayList<String> items, int index) throws checkException
    {
        throw new checkException("Not Found");
    }       
} 
  • 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-10T23:19:21+00:00Added an answer on June 10, 2026 at 11:19 pm

    As I assume you are just learning to program I will point out 3 areas of improvement

    • Much like how mathematics has BIDMAS which determines the order of evaluation of an expression Java and other program languages evaluate statements in a particulate way. This means within the Parentheses of a function you may include a statment instead of a variable or constant. This will be evaluated with the result (or return) been passed into the called function. This is why MvG says you can do lines.get(index - 1)

    • Not all exceptions you should consider and plan around will the compiler inform you about. For example in your code an invalid input for line number or word number is entered you will get a Runtime Exception (array index out of bound)

    • Naming of variables should be useful, you have index and index1. What’s the difference? I assume from reading your code one should be the user selected index of the line number and the second should be the index of the word on said line. May I suggest requestedLineIndex and requestedWordIndex.

    On a final note this is not a usual StackOverflow question hence why your question has been ‘voted down’. If you are learning as part of a course is there a course forum or Virtual Learning Environment (VLE) you can post questions on? The support of your peers at the same level of learning tends to help with exploring the basics of a language.

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

Sidebar

Related Questions

I have a file called !input.txt containing multiple lines, each line is a random
Write a program which reads a text file called input.txt which contains an arbitrary
I'm writing a program that reads huge file (3x280 GB) and does a fitting
This program reads emails (really just a .txt file structured like an email) and
Hi I have made a program that reads a text file containing words and
I have a program that reads a file, treat it and put the results
I have a small java program that reads a file in, in eclipse i
I have a program that reads from a file that grabs 4 bytes from
My SSIS program reads as input from a .csv file. The file has about
I have a program which reads an XML file using the DOM functions: $doc

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.