简体   繁体   English

Java 不会停止程序运行但会返回错误的问题

[英]Java issue that doesn't stop the program from running but will return errors

package com.Text.Scanner.java;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class TextScanner {
    public static void main(String ...args) throws IOException{
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter names for parsing");
        String input = sc.nextLine();
        ArrayList<String> names = new ArrayList<String>();
        for (int i = 0;i<=input.length();i++) {
            names.add(input.substring(0, input.indexOf(",")));
            input = input.substring(input.indexOf(",")+1); 
        }

I had issues here but I didn't think much of it, could be this我在这里遇到了问题,但我没想太多,可能是这个

        System.out.println(names);
        // handles the string import to arraylist

        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader("file location"));
            //finds file
            String line = reader.readLine();
            //reads line
            while (line != null) {
                for (int i = 0; i <= line.length(); i++) {
                    if (line.contains(names.get(i))) {
                        //gets name from array to scan line for
                        System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                        //controls length
                        line = reader.readLine();
                        }
                    }
                }
            reader.close();
           }
                catch (IOException e) {
             e.printStackTrace();
        }
    }
}

It returns this:它返回这个:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:373)
    at java.base/java.util.ArrayList.get(ArrayList.java:426)
    at com.Text.Scanner.java.TextScanner.main(TextScanner.java:32)

My goal is to setup a program that will scan the text file for a list of names (first and last) and then return the data associated with them.我的目标是设置一个程序,该程序将扫描文本文件以查找名称列表(第一个和最后一个),然后返回与它们关联的数据。

The problem was here:问题出在这里:

                for (int i = 0; i < line.length(); i++) {
                if (line.contains(names.get(i))) {
                    //gets name from array to scan line for
                    System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                    //controls length
                    line = reader.readLine();
                    }
                }

The reason why it didn't work is because the length of line is bigger than the arraylist, the solution is to do this instead:它不起作用的原因是因为线的长度大于 arraylist,解决方案是这样做:

                for (int i = 0; i < names.size(); i++) {
                if (line.contains(names.get(i))) {
                    //gets name from array to scan line for
                    System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                    //controls length
                    line = reader.readLine();
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium 程序不停止运行 - Selenium program doesn't stop running BoxCars Java 程序 - 初学者。 奇怪的问题:程序不在控制台中运行,但它在 Eclipse Java 中没有列出任何错误 - BoxCars Java Program - Beginner. Weird Issue: Program doesn't run in the console but, it lists no errors in Eclipse Java BoxCars Java 程序 - 初学者。 奇怪的问题:程序不在控制台中运行,但它在 Eclipse 中没有列出任何错误 - BoxCars Java Program - Beginner. Weird Issue: Program doesn't run in the console but, it lists no errors in Eclipse Java程序不返回Int - Java Program Doesn't Return Int 从Java程序运行git命令的问题 - issue with running git commands from java program 从批处理脚本运行Java程序时出现问题 - Issue running a java program from a batch script Junit测试,不会停止在ArrayList返回类型方法上运行 - Junit test, doesn't stop running at a ArrayList return type method Java ProcessBuilder() 运行一个程序,但该程序不返回任何输出 - Java ProcessBuilder() runs a program, but the program doesn't return any output 如果使用java的文件夹中不存在文件,如何停止程序? - How to stop a program if file doesn't exist in a folder using java? Java Swing-没有错误或异常,但程序似乎无法正常工作 - Java Swing— No errors or exceptions, but program doesn't seem to be working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM