Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

all the students who are enrolled in a course specified by the user

This is the question i tried doing both of them however i kept on getting an error saying ArraylangIndexBoundException :10.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
212 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this

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

    }
}

Input

AWQ2O0ECHC2D0ECHV2O0BENG2D0DFSF2D0AGLC2O0BMPM2D0APPL2OMBSNC2D0C

Output

There students in enrolling in awq2o0echc2d0echv2o0beng2d0dfsf2d0aglc2o0bmpm2d0appl2ombsnc2d0c are Amath Brandon (522442)
 There are 1 students in awq2o0echc2d0echv2o0beng2d0dfsf2d0aglc2o0bmpm2d0appl2ombsnc2d0c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...