Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法是表达式的非法开始_Java_Methods - Fatal编程技术网

Java 方法是表达式的非法开始

Java 方法是表达式的非法开始,java,methods,Java,Methods,下面是一些试图在一定数量的字符上方查找第一个单词的代码 public class FirstMatch { public static void main(String[] args) throws java.io.FileNotFoundException { Scanner in = new Scanner(new FileReader("aliceInWonderland.txt")); String longWord = ""; b

下面是一些试图在一定数量的字符上方查找第一个单词的代码

public class FirstMatch {
    public static void main(String[] args) throws java.io.FileNotFoundException {
        Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));
        String longWord = "";
        boolean found = false;

        public void threshold (int Threshold) {
            while (in.hasNext() && !found) {
                String word = in.next();
                if (word.length() > Threshold) {
                    longWord = word;
                    found = true;
                }
            }
            System.out.println("The first long word is: " + longWord);
        }
    }
}
(在上面的代码中,我没有在所有导入语句中复制)
由于我的阈值方法的某些原因,它返回表达式的非法开始。我认为这是一个愚蠢的错误,但无法找出问题所在……

您应该在
主方法中声明您的
阈值
方法

方法只能在类级别创建,而不能在另一个方法内部创建

import java.io.FileReader;
import java.util.Scanner;

public class FirstMatch {

    public static void main(String[] args) throws java.io.FileNotFoundException {
        Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));    
        threshold(in, 10);
    }

    public static void threshold(Scanner in, int threshold) {
        String longWord = "";
        boolean found = false;
        while (in.hasNext() && !found) {
            String word = in.next();
            if (word.length() > threshold) {
                longWord = word;
                found = true;
            }
        }
        System.out.println("The first long word is: " + longWord);
    }

}

您应该在
main
方法中声明
threshold
方法

方法只能在类级别创建,而不能在另一个方法内部创建

import java.io.FileReader;
import java.util.Scanner;

public class FirstMatch {

    public static void main(String[] args) throws java.io.FileNotFoundException {
        Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));    
        threshold(in, 10);
    }

    public static void threshold(Scanner in, int threshold) {
        String longWord = "";
        boolean found = false;
        while (in.hasNext() && !found) {
            String word = in.next();
            if (word.length() > threshold) {
                longWord = word;
                found = true;
            }
        }
        System.out.println("The first long word is: " + longWord);
    }

}

您应该在
main
方法中声明
threshold
方法

方法只能在类级别创建,而不能在另一个方法内部创建

import java.io.FileReader;
import java.util.Scanner;

public class FirstMatch {

    public static void main(String[] args) throws java.io.FileNotFoundException {
        Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));    
        threshold(in, 10);
    }

    public static void threshold(Scanner in, int threshold) {
        String longWord = "";
        boolean found = false;
        while (in.hasNext() && !found) {
            String word = in.next();
            if (word.length() > threshold) {
                longWord = word;
                found = true;
            }
        }
        System.out.println("The first long word is: " + longWord);
    }

}

您应该在
main
方法中声明
threshold
方法

方法只能在类级别创建,而不能在另一个方法内部创建

import java.io.FileReader;
import java.util.Scanner;

public class FirstMatch {

    public static void main(String[] args) throws java.io.FileNotFoundException {
        Scanner in = new Scanner(new FileReader("aliceInWonderland.txt"));    
        threshold(in, 10);
    }

    public static void threshold(Scanner in, int threshold) {
        String longWord = "";
        boolean found = false;
        while (in.hasNext() && !found) {
            String word = in.next();
            if (word.length() > threshold) {
                longWord = word;
                found = true;
            }
        }
        System.out.println("The first long word is: " + longWord);
    }

}


我为什么要这么做?导致错误的原因是什么?@Sruli public static void main(字符串[]args)是一个方法。您不能在另一个方法中创建方法——这就是您试图使用“public void threshold(int threshold)”所做的。@Sruli在Java方法中只能在类级别声明,不能在不同的方法中声明。在方法声明中,不允许有内部方法声明。方法声明是类成员(类似于字段),应该直接位于类括号
{member declarations}
下。为什么我必须这样做?导致错误的原因是什么?@Sruli public static void main(字符串[]args)是一个方法。您不能在另一个方法中创建方法——这就是您试图使用“public void threshold(int threshold)”所做的。@Sruli在Java方法中只能在类级别声明,不能在不同的方法中声明。在方法声明中,不允许有内部方法声明。方法声明是类成员(类似于字段),应该直接位于类括号
{member declarations}
下。为什么我必须这样做?导致错误的原因是什么?@Sruli public static void main(字符串[]args)是一个方法。您不能在另一个方法中创建方法——这就是您试图使用“public void threshold(int threshold)”所做的。@Sruli在Java方法中只能在类级别声明,不能在不同的方法中声明。在方法声明中,不允许有内部方法声明。方法声明是类成员(类似于字段),应该直接位于类括号
{member declarations}
下。为什么我必须这样做?导致错误的原因是什么?@Sruli public static void main(字符串[]args)是一个方法。您不能在另一个方法中创建方法——这就是您试图使用“public void threshold(int threshold)”所做的。@Sruli在Java方法中只能在类级别声明,不能在不同的方法中声明。在方法声明中,不允许有内部方法声明。方法声明是类成员(类似于字段),应该直接位于类括号
{member declarations}
下。为什么您会认为可以在另一个类中实现方法(除非它不是任何匿名类或内部类的一部分)?可能是Java的新手,在一种支持的语言中工作过。为什么你会认为你可以在另一种语言中实现一个方法(除非它不是任何匿名类或内部类的一部分)?可能是Java的新手,在一种支持的语言中工作过。为什么你会认为你可以在另一种语言中实现一个方法(除非它不是任何匿名类或内部类的一部分)?可能是Java的新手,使用过支持的语言。为什么你会认为你可以在另一个类中实现一个方法(除非它不是任何匿名类或内部类的一部分)?可能是Java的新手,使用过支持的语言。