2014-11-25 133 views
0

好的,所以我正在爲java做一個家庭作業。下面是分配: 編寫發現使用下面的頭中的字符串指定的字符的出現的次數的方法: 公共靜態詮釋計數(字符串str,炭A)計算出現次數

收件,提示的測試程序用戶輸入一個字符串後跟一個字符,並顯示該字符在字符串中出現的次數。

我的代碼中的錯誤是這樣的: char a = input.nextChar;它說:「nextChar不能得到解決或不是場」

這裏是我的代碼:

import java.util.*; 

public class CountOccurances { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner input = new Scanner (System.in); 
     System.out.println("Enter a string: "); 
     String str = input.nextLine(); 

     System.out.println("Enter a character: "); 
     char a = input.nextChar; 
     int letterCheck = count(str, a); 

     System.out.println("The character " + a + "appeared" + letterCheck + "times in" + str); 
    } 

    public static int count(String str, char a) 
    { 
     int count = 0; 
     for (int i = 0; i < str.length(); i++) 
     { 
      if (str.charAt(i) == a) 
      { 
       count++; 
      } 
     } 
     return count; 
    } 
} 
+0

任何幫助將不勝感激!提前致謝! – Tammy 2014-11-25 00:11:01

+2

[This](http://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner)可能很有趣 – Reimeus 2014-11-25 00:14:38

回答

2
char a = input.nextChar; 

您正嘗試調用上述行中的方法,而不是調用方法的方式。方法應該是這樣調用:

char a = input.nextChar(); 

Scanner類沒有一個nextChar()方法。因此,您可以使用Scanner.nextLine()來讀取一行並獲取該行的第一個字符,如下所示。

System.out.println("Enter a character:"); 
char a = input.nextLine().toCharArray()[0]; 

這裏input.nextLine()讀取一行即String

toCharArray()String讀取轉換爲字符數組。例如:

考慮讀取字符串是"HAI"它將被轉換爲{'H', 'A', 'I'}

input.nextLine().toCharArray()[0]將獲得該字符數組的第一個索引。在這個例子中,它將是'H'。但在你的情況下,數組只有一個元素,因爲你正在閱讀字符。

+0

謝謝你SOOOOOOOOOO很多!它現在完美運行! – Tammy 2014-11-25 00:28:02

4
char a = input.nextChar; 

需求是

char a = input.nextChar(); 

在你是說第一種情況input有一個名爲nextChar的靜態變量,這不是你想要的。您正嘗試通過在最後添加()來調用input對象上的方法。

即使這樣說也不行,因爲Scanner類首先沒有這個方法。什麼,你會想要做的就是這樣的事情

System.out.println("Enter a string: ");    //Prompt for string 
String str = input.nextLine();      //Get the string 
System.out.println("Enter a character: ");   //Prompt for char 
String userInput = input.nextLine();    //Get the char AS A STRING 
int letterCheck = count(str, userInput.charAt(0)); //Call your method with the first 
                //char entered 

當用戶輸入要計算的字符,你將需要獲得與nextLine()方法返回一個String該輸入。然後可以通過傳遞該字符串的第一個字符作爲第二個參數來調用您的方法。

+0

'Scanner'沒有'nextChar()'方法。 – ajb 2014-11-25 00:15:45

2

你可能打算說input.nextChar(),因爲你試圖調用一個方法。但這不會有幫助,因爲Scanner沒有nextChar()方法。

什麼你可能需要做的是:

String inputLine = input.nextLine(); 
char a = inputLine.charAt(0); 

,輸入從用戶的整條生產線,然後設置a從輸入字符串的第一個字符。 (這也將拋出一個異常,如果用戶點擊Enter鍵,而無需輸入字符。charAt會拋出異常,如果沒有字符,返回到真正的安全,你會想這樣的事情:)

char a; 
while (true) { 
    String inputLine = input.nextLine(); 
    if (inputLine.length() > 0) { 
     a = inputLine.charAt(0); 
     break; 
    } else { 
     System.out.println("Your input string is empty"); 
    } 
} 
+1

我不認爲'String'有一個方法'getChar()'(從你的第一個代碼片段)。另外,你的第二個代碼片段顯示了你在Scanner對象'input'上使用'getChar()',它也沒有這個方法。 – csmckelvey 2014-11-26 19:57:02

+0

糟糕。固定。感謝您指出錯誤。 – ajb 2014-11-26 21:15:53

+0

你有我的背,我有你的。 – csmckelvey 2014-11-26 21:16:21