Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

java - Getting an error while building strings and adding them to an ArrayList

I'm working on a final project and here's the info of the project and the errors I'm getting:

We are supposed to simulate a life form named "compAlien" and see how they react with each other. The way that these aliens are created is with a genetic code that is 128 characters long and only consists of letter X,Y or Z.

According to a couple of rules we are supposed to calculate and find such information about these aliens (Eg. if there is 20 X's in the genetic code it's eye color is going to blue etc.), create a string that is containing this information and it to a ArrayList. I was able to create the code (given below) but most of the times when I try to create a multiple aliens I always get an error message given:

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
 String index out of range: 128     at
 java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)  at
 java.base/java.lang.String.charAt(String.java:709)     at
 compAlien.calculateHealth(compAlien.java:75)   at
 compAlien.generateCompAlienInfos(compAlien.java:34)    at
 compAlien.main(compAlien.java:193)

Here's the code I was able to write:

import java.util.Scanner;
import java.util.ArrayList;

public class compAlien {
    
    static Scanner input = new Scanner(System.in);
    static ArrayList<String> compAlienInfos = new ArrayList<String>();
    static String geneticCode = "";
    static String compAlienInfo = "";
    static int colonySize = 0;
    static int count = 0;
    
    public static void generateGeneticCode() { //Method for creating the genetic codes of the compAliens.
            
        for(int i = 0; i < 128; i++) {
            
            char geneticCodeChar = (char) ('X' + Math.random() * ('Z' - 'X' + 1));
            geneticCode += geneticCodeChar;
            
        }
        
    }
    
    public static void generateCompAlienInfos() { //Method for creating all the info of the compAliens, assigning them an ID and adding their info to the compAlienInfos ArrayList.
        
        while(count < colonySize) {
            
            geneticCode = "";
            compAlienInfo = "";
            compAlienInfo += "ID:" + (count + 1) + ", ";
            
            generateGeneticCode();
            findGender();
            calculateHealth();
            findSkinTone();
            findEyeColor();
            
            compAlienInfos.add(compAlienInfo);
            System.out.println(compAlienInfo);
            count ++;
            
        }
        
    }
    
    public static void findGender() { //Method for finding the gender of the compAliens.
        
        if(geneticCode.charAt(127) == 'X') {
            
            compAlienInfo += "Gender: Female, ";
            
        }
        
        else if(geneticCode.charAt(127) == 'Y') {
            
            compAlienInfo += "Gender: Male, ";
            
        }
            
        else {
            
            compAlienInfo += "Gender: Female, ";
            
        }   
        
    }
    
    public static void calculateHealth() { //Method for calculating the health of the compAliens.
        
        int health = 1;
        int i = 0;
        
        while(i < 127) {
            
        if(geneticCode.charAt(i) == 'Y' && geneticCode.charAt(i + 1) == 'X' && geneticCode.charAt(i + 2) == 'Z') {
            
            health ++;
        
            }
        
        i++;
        
        }
        
        compAlienInfo += "Health: " + health + ", ";
        
    }
    
    public static void findEyeColor() { //Method for finding the eye color of the compAliens.
        
        int xCount = 0;
        
        for(int i = 0; i < 128; i++) {
            
            if(geneticCode.charAt(i) == 'X') {
                
                xCount ++;
                
            }
            
        }
        
        if(xCount <= 10) {
            
            compAlienInfo += "Eye Color: Green";
            
        }
        
        else if (10 < xCount && xCount <= 25) {
            
            compAlienInfo += "Eye Color: Blue";
            
        }
        
        else {
            
            compAlienInfo += "Eye Color: Brown";
            
        }
        
    }
    
    public static void findSkinTone() { //Method for finding the skin tone of the compAliens.
        
        int yCount = 0;
        
        for(int i = 0; i < 128; i++) {
            
            if(geneticCode.charAt(i) == 'Y') {
                
                yCount ++;
                
            }
            
        }
        
        if(20 <= yCount && yCount < 30) {
            
            compAlienInfo += "Skin Tone: Red ";
            
        }
        
        else if (30 <= yCount && yCount <= 40) {
            
            compAlienInfo += "Skin Tone: Green, ";
            
        }
        
        else {
            
            compAlienInfo += "Skin Tone: Orange, ";
            
        }
        
    }

    public static void main(String[] args) {
        
        System.out.println("Enter the population size of the compAlien colony: ");
        colonySize = input.nextInt();
        
        System.out.println("Simulating the compAlien colony...");
        generateCompAlienInfos();

    }
    
}

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

1 Reply

0 votes
by (71.8m points)

according to the stack trace, in the calculateHealth() method, this line is trying to access a char in the String for a position that does not exist. More specifically the charAt() call, like below. Potentially the index is greater than the geneticCode String length.

if (geneticCode.charAt(i) == 'Y' && geneticCode.charAt(i + 1) == 'X' && geneticCode.charAt(i + 2) == 'Z')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...