2013-08-17 64 views
0

問題狀態如下:給定字符串和用戶字符查找字符(由用戶給出)在字符串中重複的次數(也由用戶給出) 。查找字符串中的重複字符

我有這樣一段代碼

public int repeticion (int s){ 
     int return = 0; 
     int cont = 0; 
     Scanner in = new Scanner(System.in); 
     System.out.println("Write a string: "); 
     String chain = in.next(); 
     System.out.println("Write the character: "); 
     String character = in.next(); 
     if (chain.contains(character)) { 
      cont = cont + 1; 
     } 
     System.out.println("The character repeats itself "+cont+"times"); 
     return return; 

但你可以看到.contains只計算一次字,它有沒有出現在字符串中的次數。

+0

'contains'只檢查** **如果字符出現在字符串,而不是**多少次它**出現。您需要迭代每個字符並將其與用戶字符進行比較。嘗試使用'yourString.charAt(index)'獲取指定索引處的字符。請記住,索引可以在從'0'到'yourString.length() - 1'的範圍內。 – Pshemo

回答

0

包含將簡單地告訴您該字符是否存在。要實際計算字符出現的次數,您需要迭代字符串並計算選定字符出現的次數。這種方法將工作:

public countACharacter(char thecharacter, String stringtocountcharactersin) { 
    int count = 0; 
    for(int i = 0; i < stringtocountcharactersin.length(); i++) { 
     if(stringtocountcharactersin.charAt(i) == thecharacter) { 
      count++; 
     } 
    } 
    return count; 
} 
3

。載有()只說一個字符的字符串中存在,而不是有多少。您需要迭代字符串的每個字符以檢查它是否等於您正在搜索的字符。

String chain = "your string"; 
int cont = 0; 
for(int i=0; i<chain.length(); i++) { 
    if(chain.charAt(i) == character) { 
     cont++; 
    } 
} 
1

你也可以反覆檢查字符是在字符串中,獲得該索引,然後從該指數的字符串結束後檢查子。我推薦以下內容:

int index = 0; 
int count = 0; 
while (chain.indexof(character, index) != -1 && index < chain.length()-1) { 
    index = chain.indexof(character, index) + 1; 
    count++; 
} 
0

//帶出字符串方法。 公共靜態無效的主要(字串[] args){

String ss="rajesh kumar"; 

    Field value = null; 
    try { 
     value = String.class.getDeclaredField("value"); 
    } catch (NoSuchFieldException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    // It's private so we need to explicitly make it accessible. 

    value.setAccessible(true); 

    try { 
     char[] values = (char[])value.get(ss); 

     //String is converted to char array with out string functions 

     for (int i = 0; i < values.length; i++) { 

      int count=0; 
      for(int j=0;j<values.length;j++) 
      { 
       if(values[i]== values[j]) 
       { 
        count++; 
       } 
      } 
      System.out.println("\n Count of :"+values[i] +"="+count); 

     } 

     System.out.println("Values "+values[1]); 
    } catch (IllegalAccessException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

}

0
import java.io.*; 

public class Duplicate { 

    public static void main(String[] args) throws IOException { 
     int distinct = 0; 
     int i = 0, j = 0; 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("Enter STRING : -"); 
     String s = br.readLine(); 
     try { 
      for (i = 0; i < s.length(); i++) { 
       while (s != null) { 
        s = s.trim(); 
        for (j = 0; j < s.length(); j++) { 
         if (s.charAt(i) == s.charAt(j)) { 
          distinct++; 
         } 
        } 
        System.out.println(s.charAt(i) + "--" + distinct); 
        String d = String.valueOf(s.charAt(i)); 
        s = s.replaceAll(d, " "); 
        distinct = 0; 
       } 
      } 
     } catch (Exception e) {} 
    } 
} 
+0

您能否爲此添加一個描述,說明您爲什麼相信這會回答問題?沒有它,很難看到你在做什麼。 –

相關問題