2017-09-22 6 views
0
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class MyFirstProject { 

    public static void main(String argv[]) throws IOException { 

    //This line will create a buffered reader that reads from the system input using an input stream reader 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 
    //input will contain the first line of input from the user 
    /** 
    * This asks a question. 
    */ 
    System.out.println("What is your input?"); 
    String input = bufferedReader.readLine(); 
    /** 
    * This returns a valediction or the input. 
    */ 
    if (input.equals("exit")) { 
     System.out.println("GOODBYE!"); 
    } 
    else { 
     System.out.println(input); 
    } 


    } 


/** 
* This counts the number of words (including spaces) in a string. 
* @param words The user input. 
* @return The number of words in a string. 
*/ 
public static String numberOfWords(String words) { 
    int count = 0; 
    if (words.length() == 1) { 
     count++; 
     return (String.valueOf(count) + "word"); 
    } 
    if (words.length() > 1) { 
     for (int i = 0; i <= words.length(); i++) { 
      if (words.charAt(i) == ' ') { 
       count++; 
      } 
     } 
     return (String.valueOf(count) + "words"); 
    } 
    else { 
     return (String.valueOf(count) + "words"); 
    } 
} 
} 

私の質問はかなりシンプルです。私のコードが入力の単語の数を返さない理由を理解するのには苦労しています。たとえば、ユーザーからのコンソールの入力が "hello world"の場合、出力は次のようになります。単語(スペースを含む)を数えるには?

入力は何ですか?
Hello Worldの
のHello World
3ワード
1スペース
11文字

+3

あなたはあなたの主な方法であなたの 'numberOfWords'関数を呼び出すことはありません。 –

+0

ここをクリックhttps://stackoverflow.com/questions/5864159/count-words-in-a-string-method –

+0

申し訳ありませんが、私はjavaを初めて使いました。私はあなたが正確に何を意味するのか理解していません。あなたはそれが主要な方法の括弧内にないと言っていますか? @OHGODSPIDERS – suuugarpie

答えて

0

あなたはあなたの主な方法であなたのnumberOfWords関数を呼び出すことはありません。

これを試してみてください:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class MyFirstProject { 

    public static void main(String argv[]) throws IOException { 

    //This line will create a buffered reader that reads from the system input using an input stream reader 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 
    //input will contain the first line of input from the user 
    /** 
    * This asks a question. 
    */ 
    System.out.println("What is your input?"); 
    String input = bufferedReader.readLine(); 
    /** 
    * This returns a valediction or the input. 
    */ 
    if (input.equals("exit")) { 
     System.out.println("GOODBYE!"); 
    } 
    else { 
     //****** HERE'S IS CHANGE! ******** 
     System.out.println(numberOfWords(input)); 
    } 


    } 


/** 
* This counts the number of words (including spaces) in a string. 
* @param words The user input. 
* @return The number of words in a string. 
*/ 
public static String numberOfWords(String words) { 
    int count = 0; 
    if (words.length() == 1) { 
     count++; 
     return (String.valueOf(count) + "word"); 
    } 
    if (words.length() > 1) { 
     for (int i = 0; i <= words.length(); i++) { 
      if (words.charAt(i) == ' ') { 
       count++; 
      } 
     } 
     return (String.valueOf(count) + "words"); 
    } 
    else { 
     return (String.valueOf(count) + "words"); 
    } 
} 
} 
+0

変更を強調表示できますか?特に初心者の方には簡単に気付くでしょう。 – Zabuza

+0

このメソッドにはエラーがありますので、あなたの答えはあまり良くありません – azro

+0

オハイ、私は見る!ありがとうございました!私は必要な出力を得られませんでしたが、私はこれで作業できると思います。再度、感謝します。 – suuugarpie

0

まず、あなたはそれがあなたの入力をi番目呼び出す必要が機能numberOfWordsの計算の結果を得ることができるように、コード内で

を参照してください。次に、BufferedReader + InputStreamReaderよりScannerを好みます構文は簡単です

public static void main(String argv[]) { 

    Scanner sc = new Scanner(System.in); 

    System.out.println("What is your input?"); 
    String input = sc.nextLine();     // change to Scanner 

    if (input.equals("exit")) { 
     System.out.println("GOODBYE!"); 
    } else { 
     System.out.println(input); 
     System.out.println(numberOfWords(input)); // add method call 
    } 
} 

私も自分のものを達成するための簡単な方法を与える:

public static String numberOfWords(String words) { 
    int nbWord = words.split("\\s").length; 
    int nbSpaces = nbWord - 1; 
    return (nbWord + nbSpaces) + " words\n" + nbSpaces + " spaces\n" + words.length() + " characters"; 
} 

words.split("\\s")スペースの上に分割し、単語の配列を作成し、その長さは、あなたが

  String input = "Hello World"; 
    String w[]=input.split(" "); 
    int words=w.length; 
    int l=input.length(); 
    System.out.println("characters "+l); 
    System.out.println("words "+words); 
    System.out.println("spaces "+(words-1)); 

が出力分割機能を使用することができますここでは、スペース

0

の数です:文字11の 言葉2 スペース1

+0

これは長さに空白を含みません – Frank

+0

それはスペースが含まれているため、答えは11です –

0

分割を使用し、結果を配列に割り当てますにトリミングするのが最初からわかるように、必要なすべての情報を得ることができます。なぜなら、単語間に空白があると仮定しているからです。この方法の実施例以下

words = words.trim(); 
     String tokens[] = words.split(" "); 

words = words.trim(); 
     String tokens[] = words.split(" "); 
     int chars =0; 
     int wordsN =0; 
     int whitespace = 0; 
     for(String s : tokens){ 
      chars+=s.length(); 
      wordsN++;  
     } 
     whitespace = wordsN-1; 
     chars += whitespace; 

     System.out.println(chars); 
     System.out.println(wordsN); 
     System.out.println(whitespace); 
関連する問題