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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:42:52+00:00 2026-06-13T19:42:52+00:00

I am trying to write a coded that reads in a text file into

  • 0

I am trying to write a coded that reads in a text file into an array list. However, I am unsure how to parse my text file into strings to correctly be put into an array list. Here is a sample text file that I need to use.

This should be the sample question.

2
one
two
three
four
0
4
6

My Java code below:

package javaapplication8;

import java.util.*;
import java.io.*;
public class JavaApplication8 {


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

    Scanner inScan = new Scanner(System.in);

    String file_name;
    System.out.print("What is the full file path name?\n>>");
    file_name = inScan.next();

    Scanner fScan = new Scanner(new File(file_name));
    int numItems = Integer.parseInt(fScan.nextLine());
    ArrayList<String> Questions = new ArrayList<String>();

    for(int i=0; i < numItems; i++)
    {
        Questions.add(fScan.nextLine());
    }

    System.out.print("The array is: " + Questions);



}
  • 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-13T19:42:53+00:00Added an answer on June 13, 2026 at 7:42 pm

    From your comment about the variable numLines below, you don’t need to parse integers from the content so you can remove the line:

    int numItems = Integer.parseInt(fScan.nextLine());
    

    Then to output every line to the array, you can use Scanner.hasNextLine():

    while (fScan.hasNextLine()) {
       questions.add(fScan.nextLine());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to write a program that reads from a text file into a
I'm trying to write a program that reads text from external file (string string
I am trying to write a list to a text file that is included
I am trying to write a program that will take a text file with
I've been trying to write code that loads a .png file, attaches hotspot information,
I am trying to read a simple text file into a String. Of course
Edit: I'm trying to convert a text file into bytes. I'm not sure if
the code that i am trying to write is supposed to read text from
I'm trying to write a function that will input information into a map from
I am trying to read a large text file (14MB), each line into 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.