Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - File handling comparing string with text file

   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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...