
-----------------------------------
slider203
Sun Apr 11, 2010 1:00 pm

Arrray Error
-----------------------------------
Hello I made a program that takes a number between 6 and 9 from the user then outputs all days with that number of letters in the name (e.g. 6 monday,sunday,friday)
I have that part working but everytime I run the program It outputs an error after
[code]
    String[] days_Week = {"Sunday", "Monday", "Tuesday", "Wednesday",
      "Thursday", "Friday", "Saturday"};
    
    int[] name_Letters = {6,6,7,9,8,6,8};
    int num_Letter = 0;
    String input = "";
    final int MIN = 6;
    final int MAX_COUNT = 9;
    final int exit = -1;
    boolean numCorrect = false;

    do
    {
      System.out.println ("Please enter a number from " + MIN +" to " + MAX_COUNT+".");
      System.out.println ("Enter "+exit+" to end.");
      input = br.readLine();
      num_Letter = Integer.parseInt (input);
      
      if (num_Letter > exit)
      {
        // This loop checks if the input is in the array
        for (int count = 0; count < MAX_COUNT; count++)
        {
          if (num_Letter == name_Letters[count])
          {
            System.out.println (days_Week[count]); 
            numCorrect = true;
          }
        }
        if (!numCorrect)
        {
          System.out.println ("Error, input is not a correct number");
          System.out.println ("Please try again.");
          System.out.println (" ");
          numCorrect = false;
        }
      } 
    }
    while (num_Letter > exit);
  }
}
[/code]

-----------------------------------
DemonWasp
Sun Apr 11, 2010 2:04 pm

RE:Arrray Error
-----------------------------------
You need to post all the source code and also post the error message. Preferably, you'd point out which line the error message lists as well.

-----------------------------------
slider203
Sun Apr 11, 2010 3:12 pm

Re: Arrray Error
-----------------------------------
Hello DemonWasp that is all of my code the only thing above that is this:

java.lang.ArrayIndexOutOfBoundsException: 7
	at arrayPrac4.main(arrayPrac4.java:56)


Thanks for your time

-----------------------------------
TheGuardian001
Sun Apr 11, 2010 3:31 pm

Re: Arrray Error
-----------------------------------
Your for loop runs from 0 to MAX_COUNT, which is 9, but there are only 7 elements in your array. You should be going through based on how many values you're storing, not the maximum value being stored.

-----------------------------------
slider203
Sun Apr 11, 2010 3:43 pm

RE:Arrray Error
-----------------------------------
Once again thanks Guardian this solved my problem. Im just not understanding arrays at all :(
