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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:41:13+00:00 2026-06-19T03:41:13+00:00

I am trying to get the first line of a file with java and

  • 0

I am trying to get the first line of a file with java and I am not sure why it is not working or why I am getting the error i am getting. This is the first time I have tried this in java.

Error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
    at getSum.main(getSum.java:33)

Code

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
public class getSum {

    public static void main(String[] args) {
            File file = new File("path/InputArray.txt");
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            DataInputStream dis = null;
            String line = null;
            try{
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);

                while(dis.available() != 0){
                    line = dis.readLine();
                }
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
            String[] splitLine = line.split("\\s+");
            int[] numbers = new int[splitLine.length];

            for(int i = 0; i< line.length(); i++){
                try{
                    numbers[i] = Integer.parseInt(splitLine[i]);
                }catch(NumberFormatException nfe){};
            }


           int amount = 0;
           System.out.print(numbers.length);
           /*amount = sumArray(0,numbers.length,numbers.length,numbers);
           System.out.print("Total: " + amount);*/
    }
  • 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-19T03:41:14+00:00Added an answer on June 19, 2026 at 3:41 am

    Look at this:

    int[] numbers = new int[splitLine.length];
    for(int i = 0; i< line.length(); i++){
        try{
            numbers[i] = Integer.parseInt(splitLine[i]);
        }catch(NumberFormatException nfe){};
    }
    

    You’re using i from 0 to line.length()… which isn’t the same as splitLine.length. I suspect you meant:

    for (int i = 0; i< splitLine.length; i++) {
    

    At that point, as both numbers and splitLine have the same length, you definitely won’t get an ArrayIndexOutOfBoundsException.

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

Sidebar

Related Questions

I am trying to get Clojure to read a file, put the first line
I'm working my first internship, still trying to get this horrible thing to compile
I am trying to get the first word in the line that matches the
When trying to get geolocation on iPhone the first time - I declined. Every
I'm trying to get this resolved in .NET 2.0 and unfortunately that is not
This is the first time I've ever tried to use JAXB for anything at
So I'm trying to get some tutorial code working, and I'm stuck. I have
I'm trying to get each element on each line in a text file so
I'm trying to get Python to a read line from a .txt file and
I'm trying to get the first paragraph height within a div and store 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.