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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:22:40+00:00 2026-05-29T16:22:40+00:00

i am here with another problem in my code since i am new to

  • 0

i am here with another problem in my code since i am new to java. my task is to read a text file that contains some 300 records and record has 13 fields . i am trying to calculate the sum of each field for example, if age is my first field them sum of the age of all 300 people and then store it in an array index.

import java.io.*;
import java.util.Vector;
public class Mean
{
private static Vector contents;

private static BufferedReader br;
private static FileInputStream inputstream;
private static FileOutputStream outputstream;

public Mean()
{
    contents = new Vector();
}
public void doDuplicationRemoval(String filename)
{
    try{
        inputstream = new FileInputStream(filename);
        br = new BufferedReader(new InputStreamReader(inputstream));
        String string = "";
        while((string = br.readLine())!= null)
        {
            String[] split = string.split(",");
            Vector vector = new Vector();
            for(int i=0; i<split.length; i++)
                vector.add(split[i].trim());
            if(!vector.contains("?"))
    {
        contents.add(split);
    }


    }
    }
    catch(Exception err){
        System.out.println(err);
    }
}

 public void doDataConv(String filename)
{
    DataConversion.readFile(contents);
    DataConversion.writeFile(filename);
}

  public static void doDataConversion(Vector contents)
{
    DataConversion.readFile(contents);
    for(int i=0; i<contents.size(); i++)
    {
        String string = "";
        String[] split = (String[])contents.get(i);
        split[0] += getAge(split[0]);
        System.out.println(split[0]);
}
}
  private static String getAge(String src)
{
    String age = src;
    return age;
}

public static void main(String [] args) {
    Mean dr;
    dr = new Mean();
    dr.doDuplicationRemoval("input.txt");
    dr.doDataConv("inp_out.txt");
dr.doDataConversion(contents);
}


}

the input is
63
67
50

my aim is to get output as 180

but am getting
6363
6767
5050

can someone help me to fix the problem.

  • 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-29T16:22:41+00:00Added an answer on May 29, 2026 at 4:22 pm

    This looks like the first problem to me:

    private static String getAge(String src)
    {
        String age = src;
        return age;
    }
    

    You’re treating the age as a string. If you want to treat it as a number, you should be parsing it (e.g. with Integer.parseInt).

    Here’s the second problem:

    String string = "";
    String[] split = (String[])contents.get(i);
    split[0] += getAge(split[0]);
    System.out.println(split[0]);
    

    That’s only ever changing the value of split[0], which is then overwritten when you reassign it in the next iteration. You need something like:

    int sum = 0;
    for(int i=0; i<contents.size(); i++)
    {
        String[] split = (String[])contents.get(i);
        sum  += getAge(split[0]); // After changing getAge to return an int
    }
    System.out.println(sum);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I'm using perl to read in a file that contains some general
I must be retarded with searching, because here's another seemingly common problem that I
Here's another problem with qt: I extend a QAbstractTableModel, but I get a compiling
After getting a helpful answer here , I have run into yet another problem:
here is the problem. I have one PC using VS2008 (SP1 ) and another
There's a piece of code that looks like this. The problem is that during
I'm new to jsp and have ran into some trouble. Initially, the jsp file
I haven't got problems with code since my last problem with interface covariance. Today
I made another post earlier about this subject but I've since changed my code
we currently experience some file writing/encoding problems that we cannot reproduce. Actually there are

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.