2016-11-28 6 views
-1

このプログラムは、文字列を入力すると、プログラムはcharAt(0)の各文字をチェックし、 to charAt(文字列の長さ-1) そして有効な文字(AZまたはaz)かどうかを調べます。非文字は、単語区切り文字と見なされます。Javaユーザの入力に基づいて配列の長さを作成する方法と配列に数値を割り当てる方法

カウンタは、次の文字が文字であるたびに増加します。最後の有効な文字(この場合はcharAt(0)としましょう)の無効文字の前に、文字が無効(数字、記号、句読点、スペースなど)で、文字が1になると、カウンタは0にリセットされます。キャラクタは無効です。最初にインデックスが0で始まる配列にカウンタを割り当て、インデックスをインクリメントして次のワード長に割り当てることができます。

このプログラムで作成された単語の数に基づいて配列を作成するにはどうすればよいですか?たとえば、次の文字列を入力すると、配列の長さは7になります。

8個のpro98gram doin8gとは何ですか?

ワード1 =ワード2 =ワード3 =ワード4 =プロ、ワード5 =グラム、ワード6 =ドイン、ワード7 = g。

また、特定の配列インデックスにnumber値を割り当てるにはどうすればよいですか?たとえば、word [a]の場合、aは0,1,2,3,4などです。プログラム。

これまで私が行ってきたことです。

import java.util.Scanner; 

public class WordCountInString { 

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    int a = 0; // Initial index is 0; 
    int[] word = new int[a]; // Creating an array. 

    String string; 
    System.out.print("Enter a String of Data Please: "); 
    string = input.nextLine(); 

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

     if (string.charAt(i) >= 'a' && string.charAt(i) >= 'z' // Check to see if charAt(i) is a letter. 
       || string.charAt(i) >= 'A' && string.charAt(i) <= 'Z') { 
      counter++; // Counter every time the next char is a letter. 
     } 

     else { 
      word[a] = counter; // Assign the array of index(a) to counter 
      a++; // Go to the next index. 
      counter = 0; // Reset the counter to create a new word. 

     } 

    } 

    for (int j = 0; j < word.length; j++) { //Prints out all the arrays. 
     System.out.println(word[j]); // Print the value of each array. 

     } 
    } 
} 
+0

ほとんどの場合、文字列を2回通らなければなりません。最初に文字数を決定します。あなたは非文字の数を数えることでこれを行うことができます。そして、 'n'が単語の数なら、あなたがする必要のあることに基づいて' new int [n] 'か' new String [n] 'を作成することができます。 'ArrayList'を使うことが許されていれば、それがどれほど大きくなるかを事前に知る必要はないので、それは良いでしょう。しかし、配列を使用する必要がある場合、これは1つの方法です。 – ajb

+0

私はまだインターフェイスについて学んでいないので、私はまだインターフェイスを使用することはできません。しかし、プログラムの実行中にエラーが発生しています。何故ですか? – Majestic

+0

@Majestic多分あなたは、あなたがあなたのコードを手助けするためにどんなエラーを出しているか教えてください。 – AxelH

答えて

0

コードで判断すると、aを++にインクリメントしようとしているときのエラーです。

word[a] = counter; // Assign the array of index(a) to counter 
a++; // Go to the next index. 

ただし、アレイのサイズは変更されません。単に配列インデックスの値をインクリメントすることはできません。これにより、まだ存在しない配列内のインデックスが検索されます。

import java.util.Scanner; 

public class QnA { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     int[] word = new int[0]; // Creating an array. 

     String string; 
     System.out.print("Enter a String of Data Please: "); 
     string = input.nextLine(); 

     // Close the scanner -- 
     input.close(); 

     //string = "What is 8this pro98gram doin8g?"; 

     int counter = 0; 
     for (int i = 0; i < string.length(); i++) { 
      //System.out.println("Test"); 
      // Check to see if charAt(i) is a letter. 
      if (Character.isLetter(string.charAt(i))) {    
       // Counter every time the next char is a letter. 
       counter++; 
      } else { 
       // Check if there are any valid characters 
       if (counter > 0) { 
        // Resize the array and add the new char count 
        word = addToArray(word, counter); 
        // Reset the counter to create a new word. 
        counter = 0; 
       } 
      } 
     } 
     /* if the last characters in the string were letters then we need to 
     * add the last count to the array 
     */ 
     if (counter > 0) { 
      // Resize the array and add the new char count 
      word = addToArray(word, counter); 
     } 
     // Prints out all the arrays. 
     for (int j = 0; j < word.length; j++) { 
      // Print the value of each array. 
      System.out.println(word[j]); 
     } 
    } 

    public static int[] addToArray(int[] array, int charCount){ 
     // Resize the array 
     array = resizeArray(array, array.length + 1); 
     // Get the length and reduce 1 to get the last array index 
     array[array.length - 1] = charCount; 

     return array; 
    } 

    public static int[] resizeArray(int[] arrayToResize, int size) { 
     // create a temp array 
     int[] tempArray = new int[size]; 

     // check if the new size is different to the new size 
     if (arrayToResize.length != size) { 
      // copy array contents to new array 
      for (int i = 0; i < arrayToResize.length; i++) { 
       tempArray[i] = arrayToResize[i]; 
      } 
     } else { 
      tempArray = arrayToResize; 
     } 
     // return copied array resized. 
     return tempArray; 
    } 
} 

私はこれをあなたのコードに組み込みました。コードの最後にある2つの関数を調べる必要があります。 addToArrayおよびresizeArray

これらは、新しいカウントを配列に追加し、配列のサイズをそれ以前のサイズよりも大きくするようにサイズ変更します。

これを行うもっとエレガントな方法は、エンドサイズが何であるかを知る必要なしに次のカウントを追加するだけで済むので、リストを使用することです。リストの詳細については、JavaDocsまたはTutorialsPointの良い例を参照してください。

+0

Davidありがとう!これは非常に役に立ちます! – Majestic

関連する問題