Skip to main content
Fix some sloppy copy mistakes, that broke the code
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 21
  • 47

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Character, Integer> counter = new HashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Character)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey(sample.charAt(Character)i)))
        counter.put(ssample.charAt(i), counter.get(ssample.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(Character character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Character, Integer> counter = new HashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Character)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey((Character)i))
        counter.put(s.charAt(i), counter.get(s.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(Character character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Character, Integer> counter = new HashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Character)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey(sample.charAt(i)))
        counter.put(sample.charAt(i), counter.get(sample.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(Character character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

added 25 characters in body
Source Link
BenVlodgi
  • 4.3k
  • 2
  • 21
  • 47

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Char<Character, Integer> counter = new HashMap<CharHashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((CharCharacter)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey((charCharacter)i))
        counter.put(s.charAt(i), counter.get(s.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(charCharacter character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Char, Integer> counter = new HashMap<Char, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Char)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey((char)i))
        counter.put(s.charAt(i), counter.get(s.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(char character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Character, Integer> counter = new HashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Character)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey((Character)i))
        counter.put(s.charAt(i), counter.get(s.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(Character character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.

Source Link
BenVlodgi
  • 4.3k
  • 2
  • 21
  • 47

Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.

Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.

String sample = "Hello World!";

// Initialization
Map <Char, Integer> counter = new HashMap<Char, Integer>();
for(int c = 'a'; c <= 'z'; c++){
    counter.put((Char)c, 0);
}

// Populate
for (int i = 0; i < sample.length(); i++){
    if(counter.containsKey((char)i))
        counter.put(s.charAt(i), counter.get(s.charAt(i)) + 1 );
}

Now anytime you want to know how many of whatever character there was just call this method

int getCount(char character){
    if(counter.containsKey(character))
        return counter.get(character);
    else return 0;
}

Note: This only will work for counting punctuation.