Click here to Skip to main content
15,915,501 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hiii i really need help


package moga.Utilities;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.HashMap;

import moga.Data.Players;

public class Configuration {
	public static int POPULATION_SIZE = 200;
	public static int GENERATIONS = 25;
	public static int TOTAL_PLAYERS;	
	
	public static final float CROSSOVER_PROBABILITY = 0.7f;
    public static final float MUTATION_PROBABILITY = 0.03f;
	public static final long BUDGET = 6000000;
	public static final int OUTSIDERS = 5;
	
	public static final HashMap<Integer , Players> PLAYERS = new HashMap<Integer , Players>();
	
	public static void generatePlayers()throws IOException {
		
		String csvFile = "src/IPL.csv/";
        String line = "";      
        BufferedReader br = null;
        
        try {
            br = new BufferedReader(new FileReader(csvFile));
            int index = 0;
            while ((line = br.readLine()) != null) {
            	PLAYERS.put(index , new Players(index , line));
            	index++;
            }
            TOTAL_PLAYERS = PLAYERS.size();	            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}	
}	





package moga.Data;

public class Players {
	
	public int playerId;
	public String playerName;
	
	public int isBatsman;
	public int isBaller;
	public int matchesPlayed;
	
	//Batting statistics
	public int totalRuns;
	public Double battingAvg;
	public Double battingStrikeRate;
	public int hundredScored;
	public int fiftiesScored;
	
	//Bowling statistics	
	public Double bowlingAvg;
	public Double bowlingStrikeRate;
	public Double bowlingEconomyRate;
	public int wicketsTaken;
	public Double oversBowled;
	public int bowlingInnings;

	public long cost;
	
	public int isCaptain;
	public int isWicketKeeper;
	public int isOutsider;
	
	public Players(int id , String s) {
		this.playerId = id;
		
		String vals[] = s.split(",");

		this.playerName = vals[0];
		
		this.isBatsman = Integer.parseInt(vals[1]);
		this.isBaller = Integer.parseInt(vals[2]);
		
		this.matchesPlayed = Integer.parseInt(vals[3]);
		
		this.totalRuns = Integer.parseInt(vals[4]);
		this.battingAvg = Double.parseDouble(vals[5]);
		this.battingStrikeRate = Double.parseDouble(vals[6]);
		this.hundredScored = Integer.parseInt(vals[7]);
		this.fiftiesScored = Integer.parseInt(vals[8]);	
		
		this.bowlingInnings = Integer.parseInt(vals[9]);
		this.oversBowled = Double.parseDouble(vals[10])/6.0;
		this.wicketsTaken = Integer.parseInt(vals[11]);
		this.bowlingAvg = Double.parseDouble(vals[12]);
		this.bowlingEconomyRate = Double.parseDouble(vals[13]);
		this.bowlingStrikeRate = Double.parseDouble(vals[14]);
		
		this.cost = Long.parseLong(vals[15]);
		this.isCaptain = Integer.parseInt(vals[16]);
		this.isWicketKeeper = Integer.parseInt(vals[17]);
		this.isOutsider = Integer.parseInt(vals[18]);	
		
	}
}




import java.io.IOException;

import org.jfree.ui.RefineryUtilities;

import moga.Algorithm.GA;
import moga.Algorithm.NSGA;
import moga.DataStructures.Population;
import moga.Utilities.Configuration;
import moga.Utilities.GraphPlot;
import moga.Utilities.Printer;

public class App {
	public static void main(String args[])throws IOException{
		
		Configuration.generatePlayers();	
		GraphPlot multiPlotGraph = new GraphPlot();		

		
		Printer.printInitialParentPopulationGeneration();
		Printer.printGeneration(1);
		
		Population parent = NSGA.preparePopulation(GA.generatePopulation());
		Population child = GA.generateChildren(parent);
		
		Population combinedPopulation;
		
		for(int generation = 1; generation <= Configuration.GENERATIONS; generation++) {
            
            Printer.printGeneration(generation + 1);
            
            combinedPopulation = NSGA.preparePopulation(GA.createCombinedPopulation(parent, child));
            parent = NSGA.getChildFromCombinedPopulation(combinedPopulation);
            child = GA.generateChildren(parent);
            
            multiPlotGraph.prepareMultipleDataset(child, generation, "gen. " + generation);
            
        }
		
		Printer.printGraphPlotAlert();
	    Printer.render2DGraph(child);
	    
	    multiPlotGraph.configureMultiplePlotter("Batting Fitness ", "Balling Fitness ", "All Pareto");
        multiPlotGraph.pack();
        RefineryUtilities.centerFrameOnScreen(multiPlotGraph);
        multiPlotGraph.setVisible(true);
        
        
		
		Printer.printAlgorithmEnd();
		
	}
}



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
	at moga.Data.Players.<init>(Players.java:40)
	at moga.Utilities.Configuration.generatePlayers(Configuration.java:34)
	at moga.App.main(App.java:17)




i got this error , what's going on here ?!!!

What I have tried:

i tried change the index number 0 to 1 or other then 0
String line="" to null still not working
Posted
Updated 12-Nov-20 10:55am

Quote:
String vals[] = s.split(",");
this.playerName = vals[0];
this.isBatsman = Integer.parseInt(vals[1]);
this.isBaller = Integer.parseInt(vals[2]);
this.matchesPlayed = Integer.parseInt(vals[3]);

Error seems to be coming from this part of code.

Somewhere the line that you read from the file does not have the entries as expected. Issue is with item in bold here:
Java
while ((line = br.readLine()) != null) {
PLAYERS.put(index , new Players(index , line));


Your code is not handling for exceptions and having basic checks. First thing, before blindly using the indexes after split in below line, you should check for the array size and if the count matches your expectation.:
Java
String vals[] = s.split(",");


A simple debugging would give you these details.

Some reference: ArrayIndexOutOfBoundsException (Java Platform SE 7 )[^]
Quote:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
 
Share this answer
 
We have no idea what line that is: it's line 40 of Players.java - but we have no idea which file that is.

Even if we did, there isn't a lot we can do without your full code running with your data and you inputs - and we don't have access to all that.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

What you are looking for is the size of the array you are indexing into and the value of the index you are using - and you can only tell that for sure while your code is running. Remember, array indexes run from 0 to the number of elements minus one, so an array with 3 elements would have indexes 0, 1, and 2; an array with 2 elements would have indexes 0, and 1; an array with 1 element would have index 0 only; and an array with 0 elements would have no valid indexes at all.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
v2
Quote:
Exception in thread "main" java.lang.arrayindexoutofboundsexception: 1

The answer is in the contain of variables, but only debugger can tell us.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900