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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:49:50+00:00 2026-06-14T18:49:50+00:00

I want to implement a program that reads a file (i.e. .txt ) and

  • 0

I want to implement a program that reads a file (i.e. .txt) and saves the file in an array (I have done this). Then I want to have a 2-dimensional array where I save only the words for every line.

For example if the file contains two lines with two words in every line I want in array[0][0] the first word of the first line and in array[0][1] to have the second word of the first line, etc.

I have the following code:

for (int i=0; i < aryLines.length; i++) {
    String[] channels = aryLines[i].split(" ");

    System.out.println("line " + (i+1) + ": ");

    for (int j=0; j < channels.length; j++){
        System.out.println("word " + (j+1) + ": ");
        System.out.println(channels[j]);
    }

    System.out.println();
}

where the aryLines contatins all the lines but I didn’t find a solution that performs what I described.

  • 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-14T18:49:51+00:00Added an answer on June 14, 2026 at 6:49 pm

    Let your 1-D array is: –

    String[] lines = new String[10];
    

    You first need to declare an array of array: –

    String[][] words = new String[lines.length][];
    

    Then iterate over it, and for each line, split it and assign it to inner array: –

    for (int i = 0; i < words.length; i++) {
        words[i] = lines[i].split("\\s+");
    }
    

    Now, the problem will be, not all words are separated by just space. They also have many punctuation that you need to consider. I would leave it to you to split it on all the punctuation.

    For e.g.: –

    "This line: - has word separated by, : and -"
    

    Now, you would need to find all the punctuation used in your sentence.


    One thing which you can do is use a Regex to match a pattern for words only, if you are not sure about what all punctuation are used in your line. And add each matched word to an arraylist.

    "\\w+"  // this regex will match one or more characters forming words
    

    Let’s see it working in the above example: –

        String str = "This line: - has word separated by, : and -";
        List<String> words = new ArrayList<String>();
    
        Matcher matcher = Pattern.compile("\\w+").matcher(str);
    
        while (matcher.find()) {
            words.add(matcher.group());
        }
    
        System.out.println(words);
    

    OUTPUT : –

    [This, line, has, word, separated, by, and]
    

    You can use this approach in your above loop that I posted.

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

Sidebar

Related Questions

I have a program that reads and draws an SVG file in a java
I have a program that is been implemented in C++ which I now want
i starter in jqgrid, i want implement inline edit in jqgrid i have this
I want implement a program that can do a a Vision-based Page Segmentation. I
I have a daemon that reads a configuration file in order to know where
This is what I want to accomplish for homework: Design and implement a program
I have a program that read urls in a file and does a gethostbyname()
I want to implement program by c# application. first of all I need web
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it

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.