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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:52:18+00:00 2026-06-18T08:52:18+00:00

I have a text file that reads: 1 2 3 4 5 6 7

  • 0

I have a text file that reads:

1 2 3 4 5

6 7 8 9 1

8 3 9 7 1

9 3 4 8 2

8 7 1 6 5

where each number is separated by a tab.

My question is, is there a simple way to sum the columns of numbers using java? I want it to sum 1+6+8+9+8, 2+7+3+3+7, etc. I am reading the file using:

 public static boolean testMagic(String pathName) throws IOException {
    // Open the file
    BufferedReader reader = new BufferedReader(new FileReader(pathName));

    // For each line in the file ...
    String line;
    while ((line = reader.readLine()) != null) {
        // ... sum each column of numbers
        }

    reader.close();
    return isMagic;
}

public static void main(String[] args) throws IOException {
    String[] fileNames = { "MyFile.txt" };

}

  • 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-18T08:52:19+00:00Added an answer on June 18, 2026 at 8:52 am

    is the number of columns known or variable? You can just create an array of sums and parse each line into ints and the add it to the correct sum.

    // For each line in the file ...
    String line;
    int sums[5]; //Change to the number of columns
    while ((line = reader.readLine()) != null) {
        String tokens[] = line.split("\t");
        for(int i = 0; i < 5; i++)
        {
           sums[i] += Integer.parseInt(tokens[i]);
        }
     }
    

    If you don’t know the number of columns beforehand use an ArrayList and do the first line differently so that you can figure out the number of columns.

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

Sidebar

Related Questions

I have a simple tab-separated text file that I want Ruby to read every
I have a simple java code that reads text csv file that contains sentences
I have a method that reads data from a comma separated text file and
I have the following simple code, that reads contents of a text file into
I have a text file formatted so that there are 3 numbers, separated by
/edited/ I'm new here. I have a text file that reads: 6 <cr> R
I have an app that reads a huge text file (about more than 100MB)
I have a small Java program that reads list of IPs from text file
I Have a program written in VB6 that reads a long text file and
We have some C# code that reads data from a text file using a

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.