Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrray Error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
slider203




PostPosted: Sun Apr 11, 2010 1:00 pm   Post subject: 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);
  }
}
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Apr 11, 2010 2:04 pm   Post subject: 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




PostPosted: Sun Apr 11, 2010 3:12 pm   Post subject: Re: Arrray Error

Hello DemonWasp that is all of my code the only thing above that is this:
code:

import java.io.*;
class arrayPrac4
{
  public static void main (String args [])
    throws java.io.IOException
  {
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));


The error message is:
Quote:

java.lang.ArrayIndexOutOfBoundsException: 7
at arrayPrac4.main(arrayPrac4.java:56)


Thanks for your time
TheGuardian001




PostPosted: Sun Apr 11, 2010 3:31 pm   Post subject: 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




PostPosted: Sun Apr 11, 2010 3:43 pm   Post subject: RE:Arrray Error

Once again thanks Guardian this solved my problem. Im just not understanding arrays at all Sad
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: