2013-03-19 224 views
3

我有一个任务,我有两个类,一个驱动程序类,它使用扫描仪实用程序从键盘读取字符串,然后记录它的字母频率。 (每个字母出现在输入字符串中的次数)。我的程序应该继续输入文本行,直到您连续输入两个返回。然后代码应该打印字母频率,然后是报告,提供最频繁的信件和它的计数(在最常见的信件的情况下,任何最频繁的信件会这样做)。此外,我的代码忽略字母大小写 - 所以大写字母以及小写字母应该被计数。代码编译,但不返回任何

我的驱动类

import java.util.*; 

public class LetterDriver{ 
    public static void main(String[] args){ 
    Scanner s = new Scanner(System.in); 
    String tScan = " "; 
    while(tScan.length() > 0){ 
     tScan = s.nextLine(); 
    } 
    } 
} 

我的实际轮廓类是

public class LetterProfile { 
    int score[] = new int [26]; 

public void countChars (String s) { 
    s.toLowerCase(); 
    char a = 'a'; 
    for (int i = 0; i < s.length(); i++) { 
     int next = (int)s.charAt(i) - (int) a; 
     if (next<26 && next >= 0) 
     score[next]++; 
    } 
} 

    public void scoreLine (String lines) { // prints letter + # of appearance 
     int index = lines.length(); 
     for (int j = 0; j<index; j++) { 
     score[j]++; 
     System.out.println(j + score[j]); 
    } 
} 

    public int largestLength() { // finds most frequent letter 
     int largest = 0; 
     int largestindex = 0; 
     for(int a = 0; a<26; a++) 
     if(score[a]>largest){ 
     largest = score[a]; 
     largestindex = a; 
     } 
     return largestindex; 

    } 


    public void printResults() { 
     largestLength(); 
     System.out.println(largestLength()); 
    } 
    } 

再次我的代码编译,当我运行它让我进入我的文字输入,但是当我回到两次都是我得到空白输出。我认为这可能与我的配置文件类没有正确从我的驱动程序类读取,但无法弄清楚什么是错的。

回答

2

你只是在你的主要方法中读取扫描仪的数据,你没有在Main方法中实例化你的类LetterProfile,所以它什么都不做。

1

你没有在任何地方使用LetterProfile!您只是阅读输入内容,但未传入LetterProfile。在您的驱动程序中实例化LetterProfile类并调用相关方法。

你的驱动程序类可能看起来像:

public class LetterDriver{ 
    public static void main(String[] args){ 
    LetterProfile letterProfile = new LetterProfile(); 
    Scanner s = new Scanner(System.in); 
    String tScan = " "; 
    while(tScan.length() > 0){ 
     tScan = s.nextLine(); 
     letterProfile.countChars(tScan); 
    } 

    // Print the result 
    letterProfile.printResults() 
    } 
} 
+0

请问为什么它是letterProfile.countChars(TSCAN);而不是s?是不是我的字符串输入? – aiuna 2013-03-19 17:33:20

+0

's'是'Scanner'对象。使用扫描仪,您正在读取变量'tScan'中的字符串,您将使用该字符串。 – Amar 2013-03-19 17:35:37

+0

好的,谢谢,我觉得司机班很好走,但我的个人档案类时髦,所以它没有返回我所需的输出.. – aiuna 2013-03-19 17:38:16

1

你只是从输入获得的数据在你的主要方法,你必须初始化你的第二个类名是LetterProfile在静态的主要方法,所以它什么都不做。 java开始从读静态变量,然后静态方法,然后其他所有事情..

1

你在哪里有所谓的LetterProfile的方法在主类?

请参阅下面的代码: -

import java.util.*; 

public class LetterDriver{ 
    public static void main(String[] args){ 
    Scanner s = new Scanner(System.in); 
    LetterProfile lp=new LetterProfile(); 
    String tScan = " "; 
    while(tScan.length() > 0){ 
     tScan = s.nextLine(); 
    } 
    lp._any_of_letter_profile_class_method(); 
    } 
} 

或者

public class LetterDriver{ 
    public static void main(String[] args){ 
    Scanner s = new Scanner(System.in); 
    LetterProfile lp=new LetterProfile(); 
    String tScan = " "; 
    while(tScan.length() > 0){ 
     tScan = s.nextLine(); 
     lp._any_of_letter_profile_class_method(); 
    } 

    } 
}