两个类中的实际和正式长度错误

时间:2013-03-16 21:44:00

标签: java

我正在为学校做这个项目。

基本上你输入任何短语,程序会将其分解为数组中特定字母的数量和最常出现的字母。

离。

I am not Santa 

Santa lives in the north pole 

Santa is not Canadian

a=10

b=0

c=1

依此类推,加上

most occuring=a

这是我的主要:

import java.util.Scanner;
public class LetterDriver{
  public static void main(String[] args){
    String[] lines = new String[3];
    Scanner scan = new Scanner(System.in);
    System.out.println("enter any phrases, then press enter");
    int pos = 0;
    String tScan = " ";
    while(tScan.length() > 0){
      tScan = scan.nextLine();
      lines[pos] = tScan;
      pos++;
    }
    LetterProfile.printResults(tScan);//ERROR, I feel it's a syntax issue here, but I can't figure it out.
  }
}

这是我的另一堂课:

public class LetterProfile{

  int cCount[] = new int [26];

  public void countChars (String s){
    s.toLowerCase();
    char a = 'a';
    for (int i =0;i < s.length();i++){
      int pos = (int)s.charAt(i) -(int) a;
      if ( pos >=0 && pos < 26){
        cCount[pos]++;
      }
    }
  }
  public int mostOccur(){
    int largest = 0;
    int largestindex = 0;
    for(int a = 0; a < 26; a++){
      if(cCount[a] > largest){
        largest = cCount[a];
      }
    }
    return (largestindex);
  }
  public void printResults(){
    System.out.println(this.mostOccur());
  }
  public void runProg(String a){
    a.countChars();
    System.out.println(mostOccur(a));  //ERROR
  }
}   

我正在为此工作约5个小时,我发现它没有任何问题。我认为该程序所需的所有部分已经存在,但我只需要更好地组织它。

它给了我这些错误:

2 errors found:
File: C:\Users\Mike\Desktop\LetterDriver.java  [line: 14]
Error: method printResults in class LetterProfile cannot be applied to given types;
  required: no arguments
  found: java.lang.String
  reason: actual and formal argument lists differ in length

File: C:\Users\Mike\Desktop\LetterProfile.java  [line: 30]
Error: method mostOccur in class LetterProfile cannot be applied to given types;
  required: no arguments
  found: java.lang.String
  reason: actual and formal argument lists differ in length

2 个答案:

答案 0 :(得分:2)

基本上你的方法printResults()不带参数,但是当你调用它时,由于某种原因你传递了一个字符串。

此:

 LetterProfile.printResults(tScan);//ERROR, I feel it's a syntax issue here, but I can't figure it out.

应该是这样的:

 LetterProfile.printResults();

事实上,你永远不会在字母简介中初始化cChars[](提示,使用构造函数)。

另一个提示,使用Scanner.nextInt()从stdin获取实际整数,这样就可以直接将它们放入数组中。

答案 1 :(得分:0)

根据错误,您似乎试图为printResultsmostOccur方法提供参数,但他们不接受任何

更改跟随

System.out.println(mostOccur(a));System.out.println(mostOccur());

LetterProfile.printResults(tScan);LetterProfile.printResults();