Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I have run into an "indexOutOfBounds" exception with this portion of the code (marked in red). Any ideas?

    Java Code:
    public class CustomerDemo {
    
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
    
    
            System.out.print("How many customers would you like to enter? ");
            while(!in.hasNextInt()){
                System.out.println("'" + in.nextLine() + "'" + " is not a valid entry. Please enter a number: ");
            }
            int numberToCreate = in.nextInt();
            Customer[] customer = new Customer[numberToCreate];
    
    
            for(int i = 1; i <= numberToCreate; i++){
                
    
                System.out.println("Please enter customer " + i + " data:");
                System.out.print("First Name: ");
                String first = in.next();
    
                System.out.print("Last Name: ");
                String last = in.next();
    
    
                System.out.print("Street: " + in.nextLine());
                String street = in.nextLine();
    
                System.out.print("City: ");
                String city = in.nextLine();
    
                System.out.print("State: ");
                String state = in.next();
    
                System.out.print("Telephone: ");
                String telephone = in.next();
                [COLOR="Red"]customer[i] = new Customer(first, last, street, city, state, telephone, i);[/COLOR]
                System.out.println();
                
    
            }
            System.out.println(customer[1].getFirstName());
        }
    }

  2. #22
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Arrays are 0 based. Try changing the loop to start at 0 and end at the arrays length -1

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2011, 06:43 AM
  2. Creating a Table with user input
    By JonniBravo in forum Eclipse
    Replies: 1
    Last Post: 09-08-2010, 01:50 PM
  3. geting user input (number)
    By kliane in forum New To Java
    Replies: 9
    Last Post: 01-17-2010, 01:25 PM
  4. Creating objects based on a String value
    By lvh in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:00 PM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 08:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •