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");
}
所有注册用户指定课程的学生
这是我尝试过这两个问题,但我一直在收到错误ArraylangIndexBoundException :10
。
答案 0 :(得分:0)
您在userClass()中将项目声明为10个元素的字符串数组,然后您尝试使用该行访问其第11个元素
java.util.Arrays.toString(items[10].split("(?<=\\G.......)"));
答案 1 :(得分:0)
String items[]=new String [10];
boolean match = false;
int count=0;
这一行总是会失败,因为Java数组从0开始,所以items [9]是最大值
java.util.Arrays.toString(items[10].split("(?<=\\G.......)"));
此外,所有数组元素都是空的。
答案 2 :(得分:0)
在您的文件中,您有11个元素,因此您的作业应该是:
String items[]=new String [11];
虽然它没有必要,但您可以在以下代码中看到:
您不需要最后一列的数组。您只需使用.contains方法来确定用户输入的字符串是否在最后一列中。
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");
}
编辑: 3.如果要使用布尔变量,则代码看起来像。
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");
}
答案 3 :(得分:0)
试试这个
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