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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:09:18+00:00 2026-06-14T12:09:18+00:00

I want to separate the elements of a text file into different arrays based

  • 0

I want to separate the elements of a text file into different arrays based of whether or not the line contains a question mark. Here is as far as I got.

    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));
    ArrayList<String> Questions = new ArrayList();
    ArrayList<String> Other = new ArrayList();

    while (fScan.hasNextLine()) 
    {
        if(fScan.nextLine.indexOf("?"))
        {
            Questions.add(fScan.nextLine());
        }

        Other.add(fScan.nextLine());
    }
  • 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-14T12:09:19+00:00Added an answer on June 14, 2026 at 12:09 pm

    Quite a few issues there

    • nextLine() actually returns the next line and moves on the scanner, so you’ll need to read once instead
    • indexOf returns an int, not a boolean, I’m guessing you’re more use to C++? You can use any of the following instead:
      • indexOf(“?”) >=0
      • contains(“?”)
      • matches(“\?”) etc.
    • please follow the java ways and use camelCase for vars…

    Code

    public static void main(String[] args) throws FileNotFoundException {
    
        Scanner scanner = new Scanner(new File("foo.txt"));
        List<String> questions = new ArrayList<String>();
        List<String> other = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.contains("?")) {
                questions.add(line);
            } else {
                other.add(line);
            }
        }
        System.out.println(questions);
        System.out.println(other);
    }
    

    foo.txt

    line without question mark
    line with question mark?
    another line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to merge array elements imported from a multi-line text file, separated by
I have a text file, I want to read this text file into 3
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
I want to make something like a horizontal line with a text in the
i want to read text file containing the data of 4X4 matrix each element
I want to enclose my text fields and other form elements in a label
I want to store some fragments of an XML file in separate files. It
I have the simplest code that I want to separate in three files: Header
I know that with a large site you would want to separate footers, menus,
I have an asp.net web app. I want to separate out a particular section

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.