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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:59:34+00:00 2026-06-04T21:59:34+00:00

Through the following code i look to club all the alphabets with starting with

  • 0

Through the following code i look to club all the alphabets with starting with the same name into one array column and in the second column i want to keep the sum of the numbers associated with the alphabets.

For example :

array_1 = { {"bat","1"},
            {"rat","2"},
            {"ball","3"},
            {"run","4"},
            {"lizard","5"}
          }

into array_2 = { {"b","4"},
                 {"r","6"},
                 {"l",5}
                }

The following code gives half correct results. The problem when it reaches ball it again adds the next alphabet starting with b and stores it as a separate value. the problem is line number 42.I have marked that.How should i impose a check that it doesn’t add the number of the alphabet once it has added.

package keylogger;
import java.util.Arrays;
public class ArrayTester {

private static int finalLength=0;
private static String firstAlphabet[][];
private String data[][] = { 
                               {"Nokia" , "7"},
                               {"Blackberry" , "1"},
                               {"Nimbus","10"},
                               {"Apple","19"},
                               {"Ami","21"},
                               {"Bug","35"},
                               {"Native","200"},
                               {"zebra","100"},
                               {"Nine","9"}

                          };  

public void calculator() {
   try {  
    // traverse the whole array
    firstAlphabet = new String[data.length][data.length]; // set the length of firstAlphabet array

    for(int i=0;i<data.length;i++) {
        firstAlphabet[i][0] = data[i][0].substring( 0, 1); // get the first alphabet
        firstAlphabet[i][1] = data[i][1];
        int k = i+1;
        int v = k;
        int t=0;
        for(int j=0;j<data.length-v;j++) {
            System.out.println("Inner for loop" + j);
            String s = data[k][0];
// line 42:
            if(firstAlphabet[i][0].compareToIgnoreCase(s.substring(0, 1))==0) { 
               System.out.println("If statement");
               firstAlphabet[i][0] = s.substring(0, 1);
               Integer z = Integer.parseInt(data[k][1]) + Integer.parseInt(firstAlphabet[i][1]);
               firstAlphabet[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[] : firstAlphabet) {
        for(String x : s) {
            System.out.println(x);
        }
    }
}
}

OUTPUT

Inner for loop0

Inner for loop1

If statement

Inner for loop2

Inner for loop3

Inner for loop4

Inner for loop5

If statement

Inner for loop6

Inner for loop7

If statement

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

If statement

Inner for loop4

Inner for loop5

Inner for loop6

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

If statement

Inner for loop4

Inner for loop5

If statement

Inner for loop0

If statement

Inner for loop1

Inner for loop2

Inner for loop3

Inner for loop4

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop3

Inner for loop0

Inner for loop1

Inner for loop2

Inner for loop0

Inner for loop1

If statement

Inner for loop0

N

226

null

null

null

null

null

null

null

B

36

null

null

null

null

null

null

null

N

219

null

null

null

null

null

null

null

A

40

null

null

null

null

null

null

null

A

21

null

null

null

null

null

null

null

B

35

null

null

null

null

null

null

null

N

209

null

null

null

null

null

null

null

z

100

null

null

null

null

null

null

null

N

9

null

null

null

null

null

null

null

If we notice the sum associated with the alphabets is correct. The only problem is repetition. i.e for example N=219 which is 200+19

  • 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-04T21:59:36+00:00Added an answer on June 4, 2026 at 9:59 pm

    Repetition is caused because you dont mark already selected Alphabet as dirty.

    So in the first loop itself N has got a final count but with your current logic when Nimbus comes under itertion, you will do the entire processing since you havent marked N as dirty.

    public void calculator() {
            List<String> marked = new ArrayList<String>();
            try {
                // traverse the whole array
                firstAlphabet = new String[data.length][2]; // set the length of first Alphabet array
                for (int i = 0; i < data.length; i++) {
                    String firstLetter = data[i][0].substring(0, 1);
                    if(marked.contains(firstLetter)){
                        continue;
                    }
                    marked.add(firstLetter);
                    firstAlphabet[i][0] = firstLetter; // get the first alphabet
                    firstAlphabet[i][1] = data[i][1];
                    int k = i + 1;
                    int v = k;
                    int t = 0;
                    for (int j = 0; j < data.length - v; j++) {
                        System.out.println("Inner for loop" + j);
                        String s = data[k][0];
                        if (firstAlphabet[i][0].equalsIgnoreCase(s.substring(0,
                                1))) { // line 42
                            System.out.println("If statement");
                            firstAlphabet[i][0] = s.substring(0, 1);
                            Integer z = Integer.parseInt(data[k][1])
                                    + Integer.parseInt(firstAlphabet[i][1]);
                            firstAlphabet[i][1] = z.toString();
                        }
                        k++;
                    }
                }
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to write data through a named pipe from one
Please look through the following code snippet - HTML <div class=aclass> <h1>This is heading
If I step through the following code the call to ReturnOne() is skipped. static
I have the following code through which i am able to retrieve phone numbers.
The following code gives a NullReferenceException since XPathSelectElement can't navigate through the XPath expression
Following code iterates through many data-rows, calcs some score per row and then sorts
I am using the following code to generate excel from sql through php.this is
I have the following code to download a URL through a proxy: proxy_handler =
i have the following code ..i need to loop through end of the file
I have the following code that's looping through files in a folder and doing

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.