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

The Archive Base Latest Questions

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

The following code separates the duplicate names into 1 column and sum of numbers

  • 0

The following code separates the duplicate names into 1 column and sum of numbers associated with the names into the second column.

Like :

   Nokia 21

   Blackberry 3

   Nimbus 30

from the array given in the program.


I want to know the final length of the array that contain these entries. In this case 3. How do i calculate that ?

package keylogger;
import java.util.ArrayList;
import java.util.List;

public class ArrayTester {

private static int finalLength = 0;
private static String Name[][];
private static String data[][] = { 
                               {"Nokia" , "7"},
                               {"Blackberry" ,"1"},
                               {"Nimbus","10"},
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"},
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"}

                          };  

public void calculator() {

    Name = new String[data.length][2];
    List<String> marked = new ArrayList<String>();
    try {
        for(int i=0;i<data.length;i++) {
            Name[i][0] = data[i][0];
            Name[i][1] = data[i][1];
            String name = data[i][0];
            if(marked.contains(name)) {

                continue;
            }
            marked.add(name);
            int k = i + 1;
            int v = k;
            for (int j = 0; j < data.length - v; j++) {
                String s = data[k][0];
                if(Name[i][0].equalsIgnoreCase(s)) {
                    Name[i][0] = s;
                    Integer z = Integer.parseInt(Name[i][1]) + Integer.parseInt(data[k][1]);
                    Name[i][1] = z.toString();
                }
                k++;
            }

        }
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

public static void main(String args[]) {
    ArrayTester o = new ArrayTester();
    o.calculator();
    for(String s[] : Name) {
        for(String x : s) {
            System.out.println(x);
        }
    }
}

}

  • 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-04T14:13:02+00:00Added an answer on June 4, 2026 at 2:13 pm

    As usual, the “problem” is poor coding. Your entire program, properly written, can be reduced to just 3 lines of code (5 if you include defining the array and printing the output):

    public static void main(String[] args) {
        String data[][] = {{"Nokia", "7"}, {"Blackberry", "1"}, {"Nimbus", "10"},
            {"Nokia", "7"}, {"Blackberry", "1"}, {"Nimbus", "10"}, {"Nokia", "7"},
            {"Blackberry", "1"}, {"Nimbus", "10"}, {"Zebra", "78"}};
    
        HashMap<String, Integer> totals = new HashMap<String, Integer>();
        for (String[] datum : data)
            totals.put(datum[0], new Integer(datum[1]) + (totals.containsKey(datum[0]) ? totals.get(datum[0]) : 0));
        System.out.println("There are " + totals.size() + " brands: " + totals);
    }
    

    Output:

    There are 4 brands: {Nimbus=30, Zebra=78, Nokia=21, Blackberry=3}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code is to save image took from camera into photo album. if ([mediaType
When I write the following code directly into my html page under a script
Possible Duplicate: Removing last comma in PHP? So I have the following code: <?php
Consider the following code: SET @SQL1 = 'SELECT * INTO #temp WHERE ...' exec(@SQL1)
Possible Duplicate: OutOfMemoryError: bitmap size exceeds VM budget :- Android I have code like
I have been writing code like the following enough a lot lately. I don't
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
I'm using the following code to add separators between my menu items: #navigation_center li:before
I have the following code. I need to create list separators before A elements,
Following code iterates through many data-rows, calcs some score per row and then sorts

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.