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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:35:06+00:00 2026-06-14T22:35:06+00:00

The code below is my attempt to read from a file of strings, read

  • 0

The code below is my attempt to read from a file of strings, read through each line until a ‘:’ is found then store + print everything after that. however The print function prints out everything that I read in from the file. Can someone spot where I’m going wrong? thanks

edit: every line is in this format “Some text here:More text here”

public void openFile() {

        try {
            scanner = new BufferedReader(new FileReader("calendar.ics"));
        } catch (Exception e) {
            System.out.println("Could not open file");

        }

    }

    public void readFile() {
        ArrayList<String> vals = new ArrayList<String>();
        String test;

        try {
        while ((line = scanner.readLine()) != null)
        {

        int indexOfComma = line.indexOf("\\:"); // returns firstIndexOf ':'
        test = line.substring(indexOfComma+1);  // test to be everything after ':'
        vals.add(test);  // add values to vals
        }
        }  catch(Exception ex){ }

            for(int i=0; i<vals.size(); i++){
         System.out.println(vals.get(i));
    }
    } 
  • 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-14T22:35:07+00:00Added an answer on June 14, 2026 at 10:35 pm

    You don’t need to escape your colon.

    line.indexOf("\\:");
    

    Change the above line to: –

    line.indexOf(":"); 
    

    Because, that will search for \\:, and if not found return the value -1.

    test = line.substring(indexOfComma+1); 
    

    So, if your indexComma is -1, which will certainly be, if your string does not contain – \\:, then your above line becomes: –

    line.substring(0);  // same as whole string
    

    As a suggestion, you should have abstract type as the type of reference when declaring your list. So, you should use List instead of ArrayList on the LHS of the List declaration: –

    List<String> vals = new ArrayList<String>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my attempt to read the machine code pointed to by a function
Below is the code I am using and the error happens when I attempt
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below fails on the line: Class.forName(oracle.jdbc.driver.OracleDriver); with the error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver The
Below is the code i use to extract data from a table to a
Im using the following code to read to consumer_key and consumer_secret from config.php, pass
Say that I have the Python code: someString = file(filename.jpg).read() How can I replicate
Code below is working well as long as I have class ClassSameAssembly in same
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
This code below allows me to find the word error in all my files

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.