Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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 比较字符串和文本文件的文件处理 public static void userClass()引发IOException 字符串数据=”; 字符串=”; System.out.println(“请输入类”); 教室=sc.nextLine().toLowerCase(); 字符串subject=教室.toUpperCase(); 布尔匹配=假; BufferedReader输入=新的BufferedReader(新文件读取器(“data.txt”); data=input.readLine(); 扫描仪=新的扫描仪(System.in); data=input.readLine(); int主观性指数=10; 字符串项[]=新字符串[11];//排 while(数据!=null) { data=input.readLine(); 如果(数据==null) 打破 项目=数据。拆分(“,”); 对于(int x=0;x10) { if(项目[10]。包含(教室)) { 匹配=真; } } } } 如果(匹配==真) { System.out.println(“有“+项[0]+”、“+项[1]+”(“+项[2]+”)”+“学生在“+教室注册”); } else if(匹配==false) { System.out.println(“本校不教授科目”+科目+); }_Java - Fatal编程技术网

Java 比较字符串和文本文件的文件处理 public static void userClass()引发IOException 字符串数据=”; 字符串=”; System.out.println(“请输入类”); 教室=sc.nextLine().toLowerCase(); 字符串subject=教室.toUpperCase(); 布尔匹配=假; BufferedReader输入=新的BufferedReader(新文件读取器(“data.txt”); data=input.readLine(); 扫描仪=新的扫描仪(System.in); data=input.readLine(); int主观性指数=10; 字符串项[]=新字符串[11];//排 while(数据!=null) { data=input.readLine(); 如果(数据==null) 打破 项目=数据。拆分(“,”); 对于(int x=0;x10) { if(项目[10]。包含(教室)) { 匹配=真; } } } } 如果(匹配==真) { System.out.println(“有“+项[0]+”、“+项[1]+”(“+项[2]+”)”+“学生在“+教室注册”); } else if(匹配==false) { System.out.println(“本校不教授科目”+科目+); }

Java 比较字符串和文本文件的文件处理 public static void userClass()引发IOException 字符串数据=”; 字符串=”; System.out.println(“请输入类”); 教室=sc.nextLine().toLowerCase(); 字符串subject=教室.toUpperCase(); 布尔匹配=假; BufferedReader输入=新的BufferedReader(新文件读取器(“data.txt”); data=input.readLine(); 扫描仪=新的扫描仪(System.in); data=input.readLine(); int主观性指数=10; 字符串项[]=新字符串[11];//排 while(数据!=null) { data=input.readLine(); 如果(数据==null) 打破 项目=数据。拆分(“,”); 对于(int x=0;x10) { if(项目[10]。包含(教室)) { 匹配=真; } } } } 如果(匹配==真) { System.out.println(“有“+项[0]+”、“+项[1]+”(“+项[2]+”)”+“学生在“+教室注册”); } else if(匹配==false) { System.out.println(“本校不教授科目”+科目+); },java,Java,注册用户指定课程的所有学生 这是我尝试同时执行这两项操作时遇到的问题,但是我不断收到一个错误,即arraylagindexboundexception:10 您在userClass()中将项声明为字符串的10个元素数组,然后尝试使用该行访问其第11个元素 public static void userClass() throws IOException String data=""; String classroom=""; System.out.println("P

注册用户指定课程的所有学生


这是我尝试同时执行这两项操作时遇到的问题,但是我不断收到一个错误,即
arraylagindexboundexception:10

您在userClass()中将项声明为字符串的10个元素数组,然后尝试使用该行访问其第11个元素

   public static void userClass() throws IOException
   String data="";
    String classroom="";
    System.out.println("Please enter the class");
    classroom = sc.nextLine().toLowerCase();
    String subject=classroom.toUpperCase();
    boolean match=false;
    BufferedReader input = new BufferedReader(new FileReader("data.txt"));
    data = input.readLine();
    Scanner scanner = new Scanner(System.in);
    data = input.readLine();
    int subjectIndex = 10;
    String items[]=new String [11]; // rows
    while(data!=null)
    {
        data=input.readLine();
        if (data==null)
            break; 
        items=data.split(",");
        for (int x=0; x<items.length; x++)
        {
            if (items.length>10)
            {
                if (items[10].contains(classroom))
                {
                    match=true;
                }
            }
        }
    }
    if (match==true)
    {
        System.out.println("There are " + items[0] + "," + items[1]+  "(" +items[2]+ ")" + " student's enrolling in" +classroom);
    }
    else if (match==false)
    {
        System.out.println("The subject " +subject+ " is not taught at our school");
    }
java.util.Arrays.toString(项[10].split(“?
这一行总是会失败,因为在Java数组中,以0开头,所以items[9]是最大值

String items[]=new String [10];
boolean match = false; 
int count=0;
java.util.Arrays.toString(项[10].split(“?
  • 在您的文件中有11个元素,因此您的作业应为:

    java.util.Arrays.toString(items[10].split("(?<=\\G.......)"));
    
    虽然这并不是必需的,但正如您在以下代码中所看到的:

  • 最后一列不需要数组。只需使用.contains方法确定用户输入的字符串是否在最后一列中

    String items[]=new String [11];
    
  • 编辑: 3.如果要使用布尔变量,则代码如下所示

    BufferedReader input = new BufferedReader(new FileReader("data.txt"));
    int count=0;    
    while ((data = input.readLine()) != null) {
        String[] items = data.split(",");
        if (items[10].toLowerCase().contains(subject)) {
            System.out.println("There students in enrolling in "  +classroom+ " are " +items[0]+ " " + items[1] + " (" + items[2] + ")");
            count++;            
        }
    }
    if (count==0)
    {
        System.out.println("There subject " + classroom+ " is not taught at our school");
    }
    
    试试这个

    boolean match=false;
    while(data!=null)
    {
        data=input.readLine();
        if (data==null)
            break;
        items=data.split(",");
        if (items[10].toLowerCase().contains(subject)) {
            System.out.println("There students in enrolling in "  +classroom+ " are " +items[0]+ " " + items[1] + " (" + items[2] + ")");
            match=true;           
        }
    }
    if (!match)
    {
        System.out.println("There subject " + classroom+ " is not taught at our school");
    }
    
    输入

    输出

    AWQ2O0ECHC2D0ECHV2O0BENG2D0DFSF2D0AGLC2O0BMPM2D0APPL2OMBSNC2D0C
    

    你需要阅读整个文件来确定谁在一个班上注册。计数++只是为了知道是否有学生在一个班上注册,如果在阅读该文件后为零,那么如果你打开:
    (data=input.readLine()),则会打印出消息:“我们学校没有教授任何科目”!=null
    中,而
    您将保存3行代码。最后查看我在3中的最新版本。最后,它将学到的经验应用到您的其他方法中。检查文件信息是否如您所示。我复制了信息并创建了一个名为data.txt的文件,即使使用您的代码也没有问题。我不知道我不知道,因为如果你有它们的话。
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class ReadFile {
    
        public static void main(String[] args) throws IOException {
    
    
            Scanner scanner = new Scanner(System.in);
            String classroom = scanner.nextLine().toLowerCase();
            BufferedReader input = new BufferedReader(new FileReader("data.txt"));
            String data = input.readLine();
            int subjectIndex = 10;
            String[] items = new String[0];
            boolean match=false;
            while (data != null) {
                items = data.split(",");
    
                if (items.length > 10) {
                    if (items[subjectIndex].toLowerCase().contains(classroom)) {
                        System.out.println("There students in enrolling in "  +classroom+ " are " +items[0]+ " " + items[1] + " (" + items[2] +")");
                        match = true;
                    }
                }
                data = input.readLine();
            }
    
            if(!match)
            {
                System.out.println("The subject " +classroom+ " is not taught at our school");
            }
    
        }
    }
    
    AWQ2O0ECHC2D0ECHV2O0BENG2D0DFSF2D0AGLC2O0BMPM2D0APPL2OMBSNC2D0C
    
    There students in enrolling in awq2o0echc2d0echv2o0beng2d0dfsf2d0aglc2o0bmpm2d0appl2ombsnc2d0c are Amath Brandon (522442)
     There are 1 students in awq2o0echc2d0echv2o0beng2d0dfsf2d0aglc2o0bmpm2d0appl2ombsnc2d0c