gpt4 book ai didi

java - 如何使用我的所有方法修复“表达式的非法开头”?

转载 作者:行者123 更新时间:2023-12-02 11:14:35 25 4
gpt4 key购买 nike

我有我的代码。我认为可以,但事实并非如此。在每种方法的开头,它总是告诉我';'。意料之中,这也是“非法表达的开始”。我不知道该如何解决。有人可以帮我解决这些错误吗?

这是错误的示例:

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");
}
}
}

}

最佳答案

在对Java进行编程时,应使用a Java IDE like Eclipse,它会指出代码中最明显的错误。

  • 您为}方法的while循环后错过了一个readFile()(这是由于Sweeper)。
  • 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");
    }
    }
    }

    关于java - 如何使用我的所有方法修复“表达式的非法开头”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767678/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com