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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:29:42+00:00 2026-06-17T04:29:42+00:00

So I can search for a string in my text file, however, I wanted

  • 0

So I can search for a string in my text file, however, I wanted to sort data within this ArrayList and implement an algorithm. Is it possible to read from a text file and the values [Strings] within the text file be stored in a String[] Array.

Also is it possible to separate the Strings? So instead of my Array having:

[Alice was beginning to get very tired of sitting by her sister on the, bank, and of having nothing to do:]

is it possible to an array as:

["Alice", "was" "beginning" "to" "get"...]

.

    public static void main(String[]args) throws IOException
    {
        Scanner scan = new Scanner(System.in);
        String stringSearch = scan.nextLine();

        BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
        List<String> words = new ArrayList<String>();

        String line;
        while ((line = reader.readLine()) != null) {                
            words.add(line);
        }

        for(String sLine : words) 
        {
            if (sLine.contains(stringSearch)) 
            {
                int index = words.indexOf(sLine);
                System.out.println("Got a match at line " + index);

            }
         }

        //Collections.sort(words);
        //for (String str: words)
        //      System.out.println(str);

        int size = words.size();
        System.out.println("There are " + size + " Lines of text in this text file.");
        reader.close();

        System.out.println(words);

    }
  • 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-17T04:29:43+00:00Added an answer on June 17, 2026 at 4:29 am

    To split a line into an array of words, use this:

    String words = sentence.split("[^\\w']+");
    

    The regex [^\w'] means "not a word char or an apostrophe"

    This will capture words with embedded apostrophes like "can’t" and skip over all punctuation.

    Edit:

    A comment has raised the edge case of parsing a quoted word such as 'this' as this.
    Here’s the solution for that – you have to first remove wrapping quotes:

    String[] words = input.replaceAll("(^|\\s)'([\\w']+)'(\\s|$)", "$1$2$3").split("[^\\w']+");
    

    Here’s some test code with edge and corner cases:

    public static void main(String[] args) throws Exception {
        String input = "'I', ie \"me\", can't extract 'can't' or 'can't'";
        String[] words = input.replaceAll("(^|[^\\w'])'([\\w']+)'([^\\w']|$)", "$1$2$3").split("[^\\w']+");
        System.out.println(Arrays.toString(words));
    }
    

    Output:

    [I, ie, me, can't, extract, can't, or, can't]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that contains lots of data arranges as String separated
On a site I'm doing a user can enter a search string like do
I can't search for a particular string, since they're all very similar, but I'd
To search for a string that has been added/removed in git history I can
I am implementing a search field where the user can type in a string
I have a search form where user can search for products within their specified
I have this query where I can search the TABLE_GLOBAL_PRODUCTS $catglobal_sql = select p.*,
I took a text file and put it in one array. However, I want
So far I have made a program that can search for a single string
This is how to parse a config file question. Basically i have a text

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.