java - 如何解决 '.class' 预期错误?

标签 java

目前,我在第 40 行遇到错误 - 需要“.class”。非常感谢任何帮助。

    import java.io.*; // For File class and FileNotFoundException
import java.util.Scanner; //For the Scanner class
import javax.swing.JOptionPane; // For the JOptionPane class
/**
 * Write a description of class PartB here.
 * 
 * @Hubble, Kieran 
 * @Version 0.1
 */
public class PartB
{
    public static void main(String[] args) throws FileNotFoundException
    {
        File file; //for file input
        Scanner inputFile; //for file input
        String fileName; //to hold a file name
        String paragraph; //to extract the letter frequencies

        //get a file name from the user.
        fileName = JOptionPane.showInputDialog("enter " + " the name of the file");

        //attempt to open the file.
        try
        {
            file = new File(fileName);
            inputFile = new Scanner(file);
            JOptionPane.showMessageDialog(null, "the file was found.");

            // read the input file, processing data one line at a time
            while(inputFile.hasNext())
            { 
                String str = inputFile.nextLine();
                System.out.println(str);
            }

            //create an Output file
            PrintWriter outputFile = new PrintWriter("crackedcode.txt");

            while(paragraph.length() > 0)
            // error is occurring on the next line (line 40)
            int[]; letterCount = new int[26];
            for (int count = 0; count < paragraph.length; count++) {
                String current = paragraph[count];
                char[] letters = current.toCharArray();
            }  

            for (int count2 = 0; count2 < letters.length; count2++) { char lett = letters[count2]; if ( (lett >= 'A') & (lett <= 'Z') ) {
                        letterCount[lett - 'A']++;
                    }
                }


            for (char count = 'A'; count <= 'Z'; count++) {
                System.out.print(count + ": " +
                    letterCount[count - 'A'] +
                    " ");
            }
            System.out.println();

            // close the input file
            inputFile.close();
        }

    catch (FileNotFoundException e)
    {
        JOptionPane.showMessageDialog(null, "file not found.");
    }

    JOptionPane.showMessageDialog(null, "done.");
    System.exit(0); //terminate program
}

}

最佳答案

删除分号

int[]; letterCount = new int[26];

应该是

int[] letterCount = new int[26];

此外,您需要用 {} 包裹 while block (从其上面的行开始)

关于java - 如何解决 '.class' 预期错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10534660/

相关文章:

java - 安全性 - 如何在 android 应用程序中实现用户之间的阻止功能,使他们无法再相互联系

java - 为什么 activity_main.xml 不在 Android Studio 中显示我的组件,为什么 28 API 不起作用?

java - 如何用 if-else 或 try-catch 结构包围 Java 语句?

java - 2个表之间的Quarkus/Hibernate关系

java - 性能 Mongodb java 驱动程序

javax.net.ssl.SSLHandshakeException : javax.net.ssl.SSLProtocolException:SSL 握手中止

java - 在java中迭代EnumMap有什么更好的方法?

java - 使用线程(启动和停止它们)

java - Swing 计时器的暂停和恢复无法正常工作

java - 我的套接字服务器循环类正确吗?