我有两个读取文件的类Project2和CMSReader。但是,在CMSReader类的setColumnsInFile方法中,永远找不到扫描仪符号。感谢帮助。

这是Project2类。

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

            public class Project2
            {
                public static void main(String[] args)
                {
                    boolean found = false;
                    Scanner sc = null;
                    String fileName = null;
                    try
                    {
                        fileName = args[0];
                    }
                    catch(ArrayIndexOutOfBoundsException e)
                    {
                        System.out.print("File not found! Please try again.");
                        System.exit(0);
                    }
                    File f = null;
                    try
                    {

                        f = new File(fileName);
                        sc = new Scanner(f);
                        found = true;
                    }
                    catch(FileNotFoundException e)
                    {
                        System.out.print("File not found!");
                    }
                CMSReader inputFile = new CMSReader();

                inputFile.readFile(args);
                System.out.println("hello");
                inputFile.setColumnsInFile(args);
                System.out.println("bye");
                }
            }


这是CMSReader类:

import java.util.*;
            import java.io.*;

            public class CMSReader
            {
                public Scanner readFile(String[] file)
                {
                    String filename = file[0];
                    Scanner sc = null;
                    File f = null;
                    try
                    {
                        f = new File(filename);
                        sc = new Scanner(f);
                    }
                    catch(IOException e)
                    {
                    }
                    return sc;
                }
                public CMSReader()
                {
                }
                public String setColumnsInFile(String[] file)
                {
                    Scanner sc = null;
                    String numberOfVariables = null;
                    try
                    {
                        sc = readFile(file);
                        boolean flag = true;
                        while(sc.hasNextline() && flag == true)
                        {
                            if(sc.nextline().equals("Columns in File: "))
                            {
                                flag = false;
                                String splitVariables = sc.nextline();
                                String[] numberOfVariablesParts = splitVariables.split(": ");
                                numberOfVariables = numberOfVariablesParts[1];
                            }
                        }
                    }
                    catch(IOException e)
                    {
                    }
                    return numberOfVariables;
                }
            }

最佳答案

您在一个地方sc.Nextline在另一个地方sc.nextline

我建议使用诸如Eclipse或Intellij之类的IDE来帮助您完成代码并编写错误的拼写。

07-26 08:45