本文介绍了线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 at myja.myja.main(myja.java:86)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package myja;

import java.awt.GridLayout;
import java.io.*;
import java.util.*;
import javax.swing.*;

class Student {
    String Name;
    int RollNo;
    int Marks;
 
    public Student() {
        Name = "";
        RollNo = 0;
        Marks = 0;
    }
 
    public Student(String Name, int RollNo, int Marks) {
        this.Name = Name;
        this.RollNo = RollNo;
        this.Marks = Marks;
    }

    Student(String string, int parseInt, int parseInt0, int parseInt1) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
 
    public String getName() {
        return Name;
    }
 
    public void setName(String Name) {
        this.Name = Name;
    }
 
    public int getRollNo() {
        return RollNo;
    }
 
    public void setRollNo(int RollNo) {
        this.RollNo = RollNo;
    }
 
    public int getMarks() {
        return Marks;
    }
 
    public void setMarks(int Marks) {
        this.Marks = Marks;
    }
 
     
}



public class myja {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       JFrame frame = new JFrame();
        GridLayout layout = new GridLayout(0,3);
        frame.setLayout(layout);
         
        try{ 
            FileReader fr = null; 
            BufferedReader br = null; 
            fr = new FileReader("D:/waq/input.txt");
            br = new BufferedReader(fr); 
            String line = br.readLine();
            String [] data = null;
          
            frame.add(new JLabel("Name"));
            frame.add(new JLabel("Roll #"));
            frame.add(new JLabel("Marks"));
            List<Student> list = new ArrayList();
             
            while(line != null){
                //data = line.split("\t");
                data = line.split(" ");
                 list.add(new Student(data[0],Integer.parseInt(data[1]),Integer.parseInt(data[2])));
                line = br.readLine(); 
            } 
 
            fr.close();
            int total =0;
            for (Student student : list) {
                System.out.println("Name: "+student.getName() + " Roll # : "+ student.getRollNo()+ " Marks : "+ student.getMarks());
                frame.add(new JLabel(student.getName()));
                frame.add(new JLabel(String.valueOf(student.getRollNo())));
                frame.add(new JLabel(String.valueOf(student.getMarks())));
                total += student.getMarks();
            }
             
             
            frame.add(new JLabel("Average Marks : " +(total/list.size())));
            System.out.println("Average: " + (total/list.size()));
            } catch(IOException e){ 
                System.out.println("File Not Found"); 
        } 
        frame.setSize(400, 200);
         
        frame.setVisible(true);
 
    }
    
}

推荐答案

data = line.split(" ");
list.add(new Student(data[0],Integer.parseInt(data[1]),Integer.parseInt(data[2])));



您没有检查该行实际上是否分成3个字段。


You did not check that the line actually splits into 3 fields.


这篇关于线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 at myja.myja.main(myja.java:86)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:52