-2

When I try running the code below I get

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7
    at java.lang.String.charAt(String.java:658)
    at main.main(main.java:27)

Java Result: 1

I still have to add in something that gets the highest value and the lowest. The program has to work through a string. The method I used below, is not from any where else, I just kinda did it that way, but the teacher hinted at using loops. Any suggestions? I think It might be because the garbage in the array.

    int highValue,lowestValue,sum;
    int random;
    int check, otherCheck;
    int a = 0,b = 0,c = 0,d = 0,e = 0;
    String passingRandomIn;

    //Create a object that generates a random number 4-8
    Random ran = new Random();

    //Generates a random number between 4 and 8
    random = ran.nextInt(5)+4;

    //Sets the randomly generated number equal to check and other check
    check = random;
    otherCheck = random;

    //Declare an array of 5 characters[0-4] inclusive
    char[] myCharArray = new char[4];

    //To string  
    passingRandomIn = Integer.toString(random);   

    //Pass in the random numbers(character per character from the string)

         switch(check){
            case 8: myCharArray[4] = passingRandomIn.charAt(4);
            check--;
            case 7: myCharArray[3] = passingRandomIn.charAt(3);
            check--;    
            case 6: myCharArray[2] = passingRandomIn.charAt(2);
            check--;    
            case 5: myCharArray[1] = passingRandomIn.charAt(1);
            check--;    
            case 4: myCharArray[0] = passingRandomIn.charAt(0);
            break;
        }



        //Add the array of characters to an array of integers
        switch(otherCheck)
        {
            case 8: a = Character.getNumericValue(myCharArray[4]);

            case 7: b = Character.getNumericValue(myCharArray[3]);

            case 6: c = Character.getNumericValue(myCharArray[2]);

            case 5: d = Character.getNumericValue(myCharArray[1]);

            case 4: e = Character.getNumericValue(myCharArray[0]);
            break;
        }


        sum = a + b + c + d + e;
        System.out.println(sum);
   }
}
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
  • `case 8: myCharArray[4] = passingRandomIn.charAt(8);` can't work as the `myCharArray` is only initialised for 4 elements (0-3) – MadProgrammer Oct 09 '14 at 04:21
  • `check = random; otherCheck = random;` Hint: you can use `random` directly rather than copying it to two other variables. – Code-Apprentice Oct 09 '14 at 04:25
  • 1
    What you are trying to do? – Gautam Savaliya Oct 09 '14 at 04:26
  • I suggest that you step away from the computer and think about the problem that you are trying to solve. You should write down, in English, the steps required to solve that problem. When you do this, don't worry about Java syntax at all. Just think about how you should solve the problem. – Code-Apprentice Oct 09 '14 at 04:28
  • Sorry I should have explained better. I am trying to take a random number between 4-8. So I will have between 4-8 random numbers taken into a string. Once into the string I'm trying to parse it into some characters and turn that into something I can get the sum of(sum of all random numbers generated). – ChrisReno1234 Oct 09 '14 at 04:31
  • I took your suggestion and wrote the sudo code. I'm gonna rewrite this thing once I take a breather. – ChrisReno1234 Oct 09 '14 at 04:34
  • @ChrisReno1234 In your String you want random number from 4-8 with length of 5 i.e. 47556 , 48867 – Gautam Savaliya Oct 09 '14 at 04:35
  • I don't know what you mean by length of 5. Could you give me an example? – ChrisReno1234 Oct 09 '14 at 04:40
  • @ChrisReno1234 : 47556 , 48867, 86754 – Gautam Savaliya Oct 09 '14 at 04:41
  • 1
    I'm confused. How should I be enterpreting your example? I think I honestly don't understand how to use the random class...And I didn't think out what I was doing. I wrote the sudo code, but I'm honestly gonna go to sleep at this point. – ChrisReno1234 Oct 09 '14 at 04:48

3 Answers3

2

random = ran.nextInt(5)+4; will always generate one digit number and you are trying to access passingRandomIn.charAt(4) of that string that is a reason for StringIndexOutOfBoundsException

If you want to use random = ran.nextInt(5)+4; then you can use only passingRandomIn.charAt(0) as your String length will be 1

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
1
//Declare an array of 5 characters[0-4] inclusive
char[] myCharArray = new char[4];

The comment and the code do not match. You allocate an array of four characters with new char[4]. You should change this to new char[5]. Note that the number used here is the size of the array, not the final index as you seem to assume.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • To OP: Array sizes start at `1`. Here is a related explanation: http://stackoverflow.com/questions/22511775/why-does-array-size-declaration-use-1-as-the-first-index – theGreenCabbage Oct 09 '14 at 04:34
0

This index doesn't exist in your array as you only created a 4 element array

case 8: myCharArray[4] = passingRandomIn.charAt(8);

  • Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(String.java:658) at main.main(main.java:31) Java Result: 1 – ChrisReno1234 Oct 09 '14 at 04:26
  • I corrected both errors. I changed the code itself. And in my compiler I changed the array to go "new char[5]" – ChrisReno1234 Oct 09 '14 at 04:27