Java错误:需要数组,但找到了java.lang.String

时间:2015-03-24 04:56:16

标签: java

因此,经过很长一段时间花在这个程序上,我终于设法让这个程序编译。经过那么努力,它突然无法运行程序。当然它编译得很好,但由于一些奇怪的原因,该程序只是没有运行。它一直说,“运行期间”。我现在正在强调很多,有人可以帮助我吗?

import java.util.Scanner;
public class Period
{

  private static String phrase;
  String[] alphabet = new String [26];
  public static void main(String [] args)
  {
    Scanner keyboard = new Scanner(System.in);
    String userInput;
    int[] letter = new int [27];
    int number = keyboard.nextInt();
    System.out.println("Enter a sentence with a period at the end.");
    userInput = keyboard.nextLine();
    userInput.toLowerCase();
  }

  public void Sorter(String newPhrase)
  {
    phrase=newPhrase.substring(0,newPhrase.indexOf("."));
  }

  private int charToInt(char currentLetter)
  {
    int converted=(int)currentLetter-(int)'a';
    return converted;
  }

  private void writeToArray()
  {
    char next;
    for (int i=0;i<phrase.length();i++)
    {
      next=(char)phrase.charAt(i);
      sort(next);
    }
  }

  private String cutPhrase()
  {
    phrase=phrase.substring(0,phrase.indexOf("."));
    return phrase;
  }

  private void sort(char toArray)
  {
    int placement=charToInt(toArray);
    if (placement<0)
    {
      alphabet[0]="1";
    }
    else
    {
      // here is one of the places it mainly occurs.
      alphabet[placement]=alphabet[placement]+1;
    }
  }

  public void entryPoint()
  {
    writeToArray();
    displaySorted();
  }

  private void displaySorted()
  {
    for (int q=0; q<26;q++)
    {
      System.out.println("Number of " + (char)('a'+q) +"'s: "+alphabet[q]);
    }
  }
}

3 个答案:

答案 0 :(得分:0)

在此行的末尾添加分号;

String[] alphabet = new String [26]

答案 1 :(得分:0)

我认为你试图计算一个以点(。)结尾的句子中的字母数。所以为了做到这一点,我对你的程序进行了一些修改,见下文

修改后的代码:

import java.util.Scanner;

public class Period {

    private static String   phrase;
    String[]                alphabet    = new String[26];

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String userInput;
        int[] letter = new int[27];
        System.out.println("Enter a sentence with a period at the end.");
        //      int number = keyboard.nextInt();
        userInput = keyboard.nextLine();
        userInput.toLowerCase();
        Period period = new Period();
        period.Sorter(userInput);
        period.entryPoint();
    }

    public void Sorter(String newPhrase) {
        phrase = newPhrase.substring(0, newPhrase.indexOf("."));
    }    

    private int charToInt(char currentLetter) {
        int converted = (int) currentLetter - (int) 'a';
        return converted;
    }

    private void writeToArray() {
        char next;
        for (int i = 0; i < phrase.length(); i++) {
            next = (char) phrase.charAt(i);
            sort(next);
        }
    }

    private String cutPhrase() {
        phrase = phrase.substring(0, phrase.indexOf("."));
        return phrase;
    }

    private void sort(char toArray) {
        int placement = charToInt(toArray);
        if (placement < 0) {
            alphabet[0] = " 1";
        } else {
            // here is one of the places it mainly occurs.
            alphabet[placement] = alphabet[placement] + 1;
        }
    }

    public void entryPoint() {
        writeToArray();
        displaySorted();
    }

    private void displaySorted() {
        for (int q = 0; q < 26; q++) {
            System.out.println("Number of " + (char) ('a' + q) + "'s: " + alphabet[q]);
        }
    }
}

<强>输出:

Enter a sentence with a period at the end.
Hello World.
Number of a's:  1 
Number of b's: null
Number of c's: null
Number of d's: null1
Number of e's: null1
Number of f's: null
Number of g's: null
Number of h's: null
Number of i's: null
Number of j's: null
Number of k's: null
Number of l's: null111
Number of m's: null
Number of n's: null
Number of o's: null11
Number of p's: null
Number of q's: null
Number of r's: null1
Number of s's: null
Number of t's: null
Number of u's: null
Number of v's: null
Number of w's: null
Number of x's: null
Number of y's: null
Number of z's: null

我在某种程度上对其进行了修改。现在您可以根据您的要求进行更改。

请注意,输入中不包含任何a句子。它显示在1前面的h,而不是a显示在{{1}}的前面。所以弄清楚你要做什么&amp;如何生成结果。

答案 2 :(得分:0)

我查看了DrJava上的一个小视频教程。

你究竟如何告诉/让DrJava运行你的程序?你在打字run Period吗?

请尝试java Period