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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:04:32+00:00 2026-06-12T14:04:32+00:00

I have to add the sum of the rows of a two dimensional array

  • 0

I have to add the sum of the rows of a two dimensional array in a column to the right side. however, when I run the code it adds the sum of all the rows in every column until it reaches the end. like this: https://i.stack.imgur.com/1QQZT.png the sum is right but is suppose to go down and just once per row.

      public static void main(String[] args) throws IOException
{
    // TODO code application logic here
    File election = new File("voting_2008.txt");
    Scanner sc = new Scanner(election);

    String[] states = new String[51];
    int[][]votes = new int[51][4];
    int[] Totalbystate = new int[votes.length];

    for (int s=0; s < 51; s++)
    {
        states[s] = sc.nextLine();
    }

    for(int c=0; c < 3; c++)
    {
        for(int s=0; s < 51; s++)
        {
            votes[s][c] = sc.nextInt();

        }

    }
    Formatter fmt = new Formatter();
    fmt.format("%20s%12s%12s%12s%21s", "State", "Obama", "McCain", "Other", "Total by state");
    System.out.println(fmt);
    for (int s=0; s < 51; s++)
    {
       fmt = new Formatter();
       fmt.format("%20s", states[s]);
       System.out.print(fmt);
       for(int c=0; c < 3; c++)
       {
           fmt = new Formatter();
           fmt.format("%12d", votes[s][c]);
           System.out.print(fmt);

       }

       for(int row=0; row < votes.length; row++)
       {   
            int sum =0;
           for (int col=0; col < votes[row].length; col++)
           {
              sum = sum + votes[row][col];

           }
          fmt = new Formatter();
            fmt.format("%21d", sum);
            System.out.print(fmt);  
       }


       System.out.println();
    }
}

}

  • 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-12T14:04:34+00:00Added an answer on June 12, 2026 at 2:04 pm
       for(int row=0; row < votes.length; row++)
       {   
            int sum =0;
            for (int col=0; col < votes[row].length; col++)
             {
              sum = sum + votes[row][col];
    
            }
            fmt = new Formatter();
            fmt.format("%21d", sum);  
            System.out.print(fmt); // You are not printing newline after each vote..
       }
    

    Try using : – System.out.println(fmt); in place of commented code..

    UPDATE :- Don’t do the above change.. Will not work.. Do the following change indeed.

    Here’s is the problem.. You’re printing your total no of votes, 51 * 51 times.. You’re having this loop inside another loop, which is already running 51 times..

    So, to solve your problem.. Remove the outer loop from the above code, and just keep it like this: –

       //for(int row=0; row < votes.length; row++) // Remove this loop
       //{ // Remove this
    
            int sum =0;
            for (int col=0; col < votes[s].length; col++)
             {
              sum = sum + votes[s][col];
    
            }
            fmt = new Formatter();
            fmt.format("%21d", sum);  
            System.out.print(fmt);
       //} // Remove this also..
    

    Value of s you are getting from the outer-most loop ..

    UPDATE: – In case you want sum of all the votes.. For all column..

    int totalSum = 0;    // Declare a variable outside it..
    for (int s=0; s < 51; s++)   // This is the place where the loop started
    {
       fmt = new Formatter();
       fmt.format("%20s", states[s]);
       System.out.print(fmt);
    
       // Other inner loops
    
        int sum =0;   // Your innerloop that calculates sum of each state..
        for (int col=0; col < votes[s].length; col++)
         {
          sum = sum + votes[s][col];
    
        }
    
        totalSum += sum;   // Add sum to `totalSum`
        fmt = new Formatter();
        fmt.format("%21d", sum);  
        System.out.print(fmt);
    
        // Continue with the outer loop
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two dimensional array, which represents columns and rows of data. I
I have the following jQuery code for finding the sum of all table rows,
I have two rows in a table. I want to add in a third
I have to add a column whose default value is not null by default
All right, here's the setup: I have a table in a form whose contents
What I have are two tables: mark_notifications mark_notifications_options The mark_notifications table contains rows of
I'm trying to add the contents of all the rows retrieved from a query.
I have to add a custom layout in a relative layout on a button
I have an add this button on the website that is rendering a border
I have to add one month to todays date and have to get date

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.