String problem!

+2 Stevan Zečević · December 21, 2014
Hi guys i study computer science and this is my 1st year so i this is my first year in programing this is task to find char in string that appears 2 times this is what i wrote so far and i really dont get what did i do wrong 

void letter(){
Scanner in=new Scanner(;
String s=in.nextLine();
int n=s.length();
Character []a=new Character[n];
char a1=0;
for(int i=0;i<a.length;i++){
a=new Character(s.charAt(i));
for(int j=0;j<a.length;j++){
for(int k=0;k<a.length;k++){


Post a Reply


Oldest  Newest  Rating
+1 Branislav Lazic · December 22, 2014
I waited for that solution to show you this solution:

import java.util.ArrayList;
import java.util.List;

public class StringDemo {
    public static void main(String[] args) {

        String str = "hello";
        char[] ar = str.toCharArray();
       // Conversion of char array to List... Not that nicely solved.
        List<Character> characters = new ArrayList<>();

        for (char c : ar) {
                .distinct() // Removes duplicate values
                .forEach(c1 -> { //Cycles through list of characters and takes characters one by one
                    long count = -> c == c1).count(); // Counts number of selected letters
                    if (count > 1) System.out.println(c1 + " - " + count); // Print if it appears more then once




My solution is almost purely based on functional approach and use of new Java 8 Stream API.
Try to learn it. Solutions like this look way better then solution with.. well... old fashioned "for loop"
+1 Stevan Zečević · December 22, 2014
Thanks man but i've fix problem myself and solution came up in dream :D i feel like i'm in matrix or something
0 baskar k · December 21, 2014

public class repeat {

public static void main(String[] args){
   char character1[] = new char[500];// array to store characters
  Scanner in=new Scanner(;// get input
String s=in.nextLine();
int string_length = s.length();// find the string length
for(int i=0; i<string_length;i++){
char letter = s.charAt(i);// get the character from the string 
character1 = letter;//array to store characters

int k=0;
for(int i=0; i<string_length;i++){
for (int j=0; j<string_length;j++){

if(character1==character1[j] && (i!=j)){
System.out.println("found "+character1+" at "+ i);
        k = k+1;// to indicate whether the character is repeated

System.out.println("repeating character not found");


/images/forum/upload/2014-12-21/391d2739cb4f6d296a87bd83dc9f1aa8.JPG        /images/forum/upload/2014-12-21/52a0e85d955d09714bbc2de8e9ab4bbc.JPG
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator