Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (31.9m points)

java - having String Index Out Of Bounds Exception while file reading

public List<SoruKelime> getAllQuestion() {
        List<SoruKelime> questions = new ArrayList<>();
        try { 
            Scanner myReader = new Scanner(myFile);

            while (myReader.hasNextLine()) {
                String data;
                data = myReader.nextLine();
                int index = data.indexOf("&");
                String question = data.substring(0, index);
                String answer = data.substring(index+1);
                SoruKelime soru = new SoruKelime();
                soru.setSoru(question);
                soru.setKelime(answer);
                questions.add(soru);
            }
            myReader.close();
        } catch (FileNotFoundException exception) {
            exception.printStackTrace();
        }
        return questions;
    }


  @Override
    public void initialize(URL url, ResourceBundle rb) {

        FileProcess fileProcess = new FileProcess();
        List<SoruKelime> sorular = new LinkedList<SoruKelime>();
        SoruKelime cekilenSoru = new SoruKelime();
        sorular = (ArrayList<SoruKelime>) fileProcess.getAllQuestion();
        cekilenSoru = sorular.get(0);

        this.soruLabel.setText(cekilenSoru.getSoru());
    
    }    


Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at word_game.FileProcess.getAllQuestion(FileProcess.java:53)
    at word_game.GameController.initialize(GameController.java:34)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    

while i'm reading a file in java with javafx i'm having this kind of issue. i've tried to change linkedList to List but that didn't work. I don't know what else I can do. Can you help me about that


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

1 Answer

0 votes
by (31.9m points)

You might have a line where there isn't an ampersand (&). Now, it is always a nice practice to assure yourself that what you're going to access exists, so you won't end up with NullPointers or IndexOutOfBounds.

        List<SoruKelime> questions = new ArrayList<>();
        try { 
            Scanner myReader = new Scanner(myFile);

            while (myReader.hasNextLine()) {
                String data;
                data = myReader.nextLine();
                int index = data.indexOf('&');
                if(index==-1 || index > data.length){
                   //There is no &, do something
                }else{
                   String question = data.substring(0, index);
                   String answer = data.substring(index+1);
                   SoruKelime soru = new SoruKelime();
                   soru.setSoru(question);
                   soru.setKelime(answer);
                   questions.add(soru);
                 }
            }
            myReader.close();
        } catch (FileNotFoundException exception) {
            exception.printStackTrace();
        }
        return questions;
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...