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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:49:36+00:00 2026-05-31T22:49:36+00:00

I am trying to read file and split its line to get some context(Computer

  • 0

I am trying to read file and split its line to get some context(Computer Name and Date), the code gives few lines of outputs then gives the following exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at FILE_MAIN.getComputerName(FILE_MAIN.java:34)
        at FILE_MAIN.readFiles(FILE_MAIN.java:24)
        at FILE_MAIN.main(FILE_MAIN.java:12)

Code:

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class FILE_MAIN
{
    public static void main(String[] args) throws FileNotFoundException
    {
        File folder = new File("folderName/");
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++)
        {
            readFiles(listOfFiles[i].getName());
        }
    }
    public static void readFiles(String fileName) throws FileNotFoundException
    {
        FileReader dataFile = new FileReader("yukt/"+fileName);
        try (Scanner FileRead = new Scanner(dataFile)) 
        {
            while (FileRead.hasNextLine() && FileRead.nextLine().isEmpty()==false)
            {
                String[] split;
                String line = FileRead.nextLine();
                split = line.split("\\|",-1);
                String computerName=getComputerName(split[0]);
                System.out.println(computerName);
            }      
        }
    }
    public static String getComputerName(String splited)
    {
        String[] split1;
        String[] split2;
        split1=splited.split("\\:",-1);
        split2=split1[1].split("\\ ",-1);
        return("a");
    }
    public static String getDate(String splited)
    {
        String[] split1=splited.split("\\(",-1);
        String[] split2=split1[1].split("\\ ",-1);
        return(split2[0]);
    }
}

The main function gets names of the files in a folder, and passes each file to the readFiles function where each line is split into 3 parts by a delimeter(|) and parts are send to getComputerName and getDate function which returns its values after further splitting the strings.

Here is an example of a line of the file, all the lines are similar to this:

[CD8C] ComputerName:NITIN UserID:GO ankurjain Station 9900  LanId: | (11/24 19:50:30) | Client is disconnected from agent.
  • 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-05-31T22:49:38+00:00Added an answer on May 31, 2026 at 10:49 pm

    There is no protection for split1 containing a single element:

    split1=splited.split("\\:",-1);
    split2=split1[1].split("\\ ",-1); // Accesses second element of split1
    

    Add protection and decide if it is an error for there to be no : in the input string or just use whole string if no ::

    split1=splited.split("\\:",-1);
    if (split1.length > 1)
    {
        split2=split1[1].split("\\ ",-1);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get Python to a read line from a .txt file and
I am trying to read a file and the error i get is java.io.FileNotFoundException:
I'm trying to read a file line by line to a string type variable
I am trying to read a file line-by-line, and storing that line of data
I'm trying to read a file one character at a time, but for some
I'm trying to read a line from a static file and insert it into
I'm trying to read some information from a text file and put in a
I'm trying to read a text file containing the name of the image, and
I am trying to read a config file which has some key value pairs
alright so I am trying to read a text file. and then split 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.