Java 找不到符号方法hasNext

Java 找不到符号方法hasNext,java,Java,我在编译时遇到了问题,因为它显示了一个“找不到符号”方法在下一行中的位置 while(x.hasNext()) 这是一个程序,用于将学生的数据存储在文件中,并通过搜索姓名显示该数据 import java.util.*; import java.io.File; import java.lang.*; import java.io.*; public class beta1_1 { private Formatter x; Scanner sc=n

我在编译时遇到了问题,因为它显示了一个“找不到符号”方法在下一行中的位置

 while(x.hasNext())
这是一个程序,用于将学生的数据存储在文件中,并通过搜索姓名显示该数据

import java.util.*;
    import java.io.File;
    import java.lang.*;
    import java.io.*;

public class beta1_1
{
    private Formatter x;

    Scanner sc=new Scanner(System.in);
    int ctr=0;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    void main()throws IOException
    {

        System.out.println("1) Add new Information");
        System.out.println("2) View Student Information");
        System.out.println("Select Your Option");
        int op=sc.nextInt();
        if(op==1)
        {
            addInformation();
        }
        if(op==2)
        {
            viewInformation();
        }

    }

    void addInformation()throws IOException
    {
        System.out.println("Number of students to be entered");
        int n=sc.nextInt();
        for(int i=0;i<n;i++)
        {
            System.out.println("Enter name: ");
            String nm=br.readLine();
            nm=nm.toUpperCase();

            System.out.println("Enter class: ");
            int c=sc.nextInt();

            System.out.println("Enter section: ");
            char s=sc.next().charAt(0);
            s=Character.toUpperCase(s);

            System.out.println("Enter address: ");
            String a=br.readLine();
            a=a.toUpperCase();

            try{
                x=new Formatter("save.txt");
            }
            catch(Exception e)
            {
                System.out.println("Error");
            }
            x.format("%s %s %s %s",nm,c,s,a,"/n");
            x.close();
        }
        System.out.println("Do you want to continue? ");
        System.out.println("Enter Y for yes or N for no");
        char d=sc.next().charAt(0);
        d=Character.toUpperCase(d);
        if (d=='N')
        {
            exit();
        }
        else
        if (d=='Y')
        {
            viewInformation();
        }
    }

    void viewInformation()throws IOException
    {
        System.out.println("Select method of search: ");
        System.out.println("1)By Name");
        System.out.println("2)By Class");
        int ch=sc.nextInt();
        if(ch==1)
        {
            System.out.println("Enter Name: ");
            String sh=br.readLine();
            sh=sh.toUpperCase();

            int i=0;


while(x.hasNext())
            {
                String a=x.next();
                String b=x.next();
                String c=x.next();
                String d=x.Next();

                if(a==sh)
                {
                    System.out.println("%s %s %s %s",a,b,c,d);
                    ctr++;
                }
            }
        }
    }

    void exit()
    {
        System.out.println("Thank You for Using");
    }
}
import java.util.*;
导入java.io.File;
导入java.lang.*;
导入java.io.*;
公共类beta1_1
{
专用格式化程序x;
扫描仪sc=新的扫描仪(System.in);
int ctr=0;
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
void main()引发IOException
{
System.out.println(“1)添加新信息”);
System.out.println(“2)查看学生信息”);
System.out.println(“选择您的选项”);
int op=sc.nextInt();
如果(op==1)
{
附加信息();
}
如果(op==2)
{
视图信息();
}
}
void addInformation()引发IOException
{
System.out.println(“要输入的学生人数”);
int n=sc.nextInt();

对于(inti=0;i您已经创建了x类型的格式化程序 如果
Formatter
是来自java util包的类,那么它没有任何
hasNext
方法,这就是您无法使用它的原因

如果您正在创建名为
Formatter
的任何类,并且其中有任何方法
hasNext
,那么您可以在代码中导入该类以使用它

hasNext()
方法属于类

java.util.Scanner.hasNext() 

然后,您必须创建x类型的扫描仪,并在其上使用
hasNext()

您不能使用Formatter类执行此操作,请尝试使用类似扫描仪的:

    String file="save.txt";        
    Scanner sc2 = null;
    try {
        sc2 = new Scanner(new File(file));
    } catch (FileNotFoundException e) {
        e.printStackTrace();  
    }
    while (sc2.hasNextLine()) {
        Scanner sc3 = new Scanner(sc2.nextLine());            
        String a=sc3.next();
        String b=sc3.next();
        String c=sc3.next();
        String d=sc3.next();           

        if (a.equals(sh)){ //have to use the .equals to compare strings
            System.out.println(a+" "+b+" "+c+" "+d);
            ctr++;
        }
    }

什么是
x
?为什么你认为它有一个
hasNext()
方法?上面说
Formatter
没有
hasNext()
方法。你想用它做什么?