Java 如何使用所有方法修复表达式的非法开头?

Java 如何使用所有方法修复表达式的非法开头?,java,compiler-errors,Java,Compiler Errors,我有密码。我认为没关系,但事实并非如此。在每个方法的开头,它总是告诉我有一个“;”预期的,它也是一个“非法开始的表达”与无效。我不知道如何修理它。有人能帮我修复这些错误吗 下面是一个错误示例: F:\COMP SCI\Topic 29 - Data Structures -- Robin Hood\Problem Set\RobinHoodApp.java:203: error: ';' expected void arrayList **()** throws FileNotFound

我有密码。我认为没关系,但事实并非如此。在每个方法的开头,它总是告诉我有一个“;”预期的,它也是一个“非法开始的表达”与无效。我不知道如何修理它。有人能帮我修复这些错误吗

下面是一个错误示例:

F:\COMP SCI\Topic 29  - Data Structures -- Robin Hood\Problem Set\RobinHoodApp.java:203: error: ';' expected
   void arrayList **()** throws FileNotFoundException();


F:\COMP SCI\Topic 29  - Data Structures -- Robin Hood\Problem Set\RobinHoodApp.java:212: error: illegal start of expression
     **void** output()


F:\COMP SCI\Topic 29  - Data Structures -- Robin Hood\Problem Set\RobinHoodApp.java:212: error: ';' expected
     void output **()**
我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.System.out;
import java.util.ArrayList;
import javax.swing.JFrame;

public class RobinHoodApp{
    public static void main(String[] args) throws FileNotFoundException, IOException    {
        RobinHood app = new RobinHood();
        app.readFile();
        app.arrayList();
        app.wordCount();
        app.countMenAtArms();
        app.writeToFile();
    }
}

class RobinHood extends JFrame
{
    private static final ArrayList<String>words = new ArrayList<>();
    private static Scanner book;
    private static int count;
    private static int wordCount;
    public RobinHood()
    {


        try {
            //        scrubber();


            //Prints All Words 1 by 1: Works!


            book = new Scanner(new File("RobinHood.txt") );
            book.useDelimiter("\r\n");

        } catch (FileNotFoundException ex)
        {
             out.println("Where's your text fam?");
        }
}

    void readFile()
    {

      while(book.hasNext())
        {
                    String text = book.next();
                    out.println(text);
        }

   void arrayList() throws FileNotFoundException();
    {
        Scanner add = new Scanner(new File("RobinHood.txt"));

        while(add.hasNext())
        {
            words.add(add.next());
        }
    }
     void output()
    {
        out.println(words);
    }

    void countMenAtArms()
    {
        //Shows 23 times
        String find = "men-at-arms";
        count = 0;
        int x;
        String text;

         for(x=0; x< wordCount; x++ )
        {
            text = words.get(x);
            text = text.replaceAll("\n", "");
            text = text.replaceAll("\n", "");
            if (text.equals(find))
            {
                count++;
            }

        }
        out.println("The amount of time 'men-at-arms' appears in the book is: " + count);
    }
//    void scrubber()
//    {
//
//    }
//
//

     void wordCount()
     {
        {
           wordCount=words.size();
           out.println("There are "+wordCount+" words in Robin Hood.");
        }
    }

    public void writeToFile()
    {
        File file;

            file = new File("Dominique.dat");
            try (FileOutputStream data = new FileOutputStream(file)) {
                if ( !file.exists() )
                    {
                        file.createNewFile();
                    }

                String wordCountSentence = "There are "+ wordCount +" words in Robin Hood. \n";
                String countTheMen = "The amount of time 'men-at-arms' appears in the book is: " + count;
                byte[] strToBytes = wordCountSentence.getBytes();
                byte[] menToBytes = countTheMen.getBytes();
                data.write(strToBytes);
                data.write(menToBytes);
                data.flush();
                data.close();
            }
            catch (IOException ioe)
            {
               System.out.println("Error");
            }
        }
    }

}
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入静态java.lang.System.out;
导入java.util.ArrayList;
导入javax.swing.JFrame;
公共级Robinhoodpp{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
RobinHood app=新RobinHood();
app.readFile();
app.arrayList();
app.wordCount();
app.countMenAtArms();
app.writeToFile();
}
}
类RobinHood扩展了JFrame
{
私有静态final ArrayListwords=new ArrayList();
私人静态扫描簿;
私有静态整数计数;
私有静态int字数;
公共RobinHood()
{
试一试{
//洗涤器();
//打印所有单词1 x 1:Works!
book=新扫描仪(新文件(“robinood.txt”);
book.useDelimiter(“\r\n”);
}捕获(FileNotFoundException ex)
{
println(“你的短信在哪里?”);
}
}
void readFile()
{
while(book.hasNext())
{
String text=book.next();
out.println(文本);
}
void arrayList()抛出FileNotFoundException();
{
扫描仪添加=新扫描仪(新文件(“robinood.txt”);
while(add.hasNext())
{
words.add(add.next());
}
}
无效输出()
{
out.println(字);
}
void countMenAtArms()
{
//显示23次
String find=“武装人员”;
计数=0;
int x;
字符串文本;
对于(x=0;x
编程Java时应该使用它,它会向您指出代码中最明显的错误


  • 您错过了
    }
    之后的
    循环,而
    readFile()
    方法的
    循环(感谢您的帮助)

  • arrayList()
    方法中的语法错误

    void arrayList() throws FileNotFoundException();   {
    
这个定义的结尾没有分号,结尾也没有括号,您描述的是类,而不是方法。以下是正确的方法:

void arrayList() throws FileNotFoundException    {
  • 在类文件的末尾添加1个无用的
    }
下面是您的代码,布局正确,没有语法错误。下次请使用IDE,这样可以避免很多麻烦

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import static java.lang.System.out;
import java.util.ArrayList;
import javax.swing.JFrame;

public class RobinHoodApp   {
    public static void main(String[] args) throws FileNotFoundException, IOException    {
        RobinHood app = new RobinHood();
        app.readFile();
        app.arrayList();
        app.wordCount();
        app.countMenAtArms();
        app.writeToFile();
    }
}

class RobinHood extends JFrame
{
    private static final ArrayList<String>words = new ArrayList<>();
    private static Scanner book;
    private static int count;
    private static int wordCount;

    public RobinHood()
    {
        try {
            // Prints All Words 1 by 1: Works!

            book = new Scanner(new File("RobinHood.txt") );
            book.useDelimiter("\r\n");

        } catch (FileNotFoundException ex)
        {
            out.println("Where's your text fam ?");
        }
    }

    void readFile()
    {
        while(book.hasNext())
        {
            String text = book.next();
            out.println(text);
        }
    }

   void arrayList() throws FileNotFoundException
   {
        Scanner add = new Scanner(new File("RobinHood.txt"));

        while(add.hasNext())
        {
            words.add(add.next());
        }
   }

     void output()
     {
        out.println(words);
     }

     void countMenAtArms()
     {
        // Shows 23 times
        String find = "men-at-arms";
        count = 0;
        int x;
        String text;

         for(x=0; x< wordCount; x++ )
         {
            text = words.get(x);
            text = text.replaceAll("\n", "");
            text = text.replaceAll("\n", "");
            if (text.equals(find))
            {
                count++;
            }
         }

         out.println("The amount of time 'men-at-arms' appears in the book is: " + count);
    }

     void wordCount()
     {
        {
           wordCount=words.size();
           out.println("There are "+wordCount+" words in Robin Hood.");
        }
    }

    public void writeToFile()
    {
        File file;

        file = new File("Dominique.dat");
        try (FileOutputStream data = new FileOutputStream(file)) {
            if ( !file.exists() )
            {
                file.createNewFile();
            }

                String wordCountSentence = "There are "+ wordCount +" words in Robin Hood. \n";
                String countTheMen = "The amount of time 'men-at-arms' appears in the book is: " + count;
                byte[] strToBytes = wordCountSentence.getBytes();
                byte[] menToBytes = countTheMen.getBytes();
                data.write(strToBytes);
                data.write(menToBytes);
                data.flush();
                data.close();
            }
            catch (IOException ioe)
            {
               System.out.println("Error");
            }
        }
    }
import java.util.Scanner;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入静态java.lang.System.out;
导入java.util.ArrayList;
导入javax.swing.JFrame;
公共级Robinhoodpp{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
RobinHood app=新RobinHood();
app.readFile();
app.arrayList();
app.wordCount();
app.countMenAtArms();
app.writeToFile();
}
}
类RobinHood扩展了JFrame
{
私有静态final ArrayListwords=new ArrayList();
私人静态扫描簿;
私有静态整数计数;
私有静态int字数;
公共RobinHood()
{
试一试{
//打印所有单词1 x 1:Works!
book=新扫描仪(新文件(“robinood.txt”);
book.useDelimiter(“\r\n”);
}捕获(FileNotFoundException ex)
{
println(“你的短信在哪里?”);
}
}
void readFile()
{
while(book.hasNext())
{
String text=book.next();
out.println(文本);
}
}
void arrayList()引发FileNotFoundException
{
扫描仪添加=新扫描仪(新文件(“robinood.txt”);
while(add.hasNext())
{
words.add(add.next());
}
}
无效输出()
{
out.println(字);
}
void countMenAtArms()
{
//显示23次
String find=“武装人员”;
计数=0;
int x;
字符串文本;
对于(x=0;xthrows FileNotFoundException();
throws FileNotFoundException