8 Replies - 37158 Views - Last Post: 19 April 2010 - 02:36 PM Rate Topic: -----

#1 JamesAngie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-February 08

Index Out Of Bounds Exception

Posted 25 March 2008 - 10:24 AM

Hello,

Okay...I fixed it so that it displayes all of the user names at the end now, but I only want it to display the last entry...any ideas....

I am trying to fix an IndexOutOfBoundsException, I am not sure what I need to do. Please look and advise. Thanks



import java.io.*;
import java.util.ArrayList;


public class UserList
{

public static void main(String[] args) throws IOException
{
//declare variables
String str1, str2 = "username";
int index;
int initialCapacity = 5;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
ArrayList users = new ArrayList();

//get user input
System.out.print("Enter a user name: ");
str1 = dataIn.readLine();

//compare user input
while(str1.length() > 0)
{
//check for "username"
if(str1 == str2)
System.out.println("That user name is NOT allowed!");
else
{
//check for capacity of 5
if(users.size() == initialCapacity)
{
System.out.println("List is full!");
//okay I got it to work but not able to list the last user just all of them
System.out.println("Last entry is " +users);
}
else//check if user is in list already
if(!users.contains(str1))
{
users.add(str1);
System.out.println("User \""+str1+"\" added to user list.");
}
else//
System.out.println("User \""+str1+"\" already in user list.");
}

System.out.print("\nEnter a user name: ");
str1 = dataIn.readLine();
}
//completion of program
System.out.println("Program complete.");

}
}

This post has been edited by JamesAngie: 25 March 2008 - 11:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Index Out Of Bounds Exception

#2 JamesAngie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-February 08

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 12:13 PM

Does anyone have any ideas about this? Thanks
Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 12:46 PM

The first problem was that the while-loop never exited.

So, the program never executed the statements outside the loop

Some of the fixes might be helpful.


import java.io.*;
import java.util.ArrayList;


public class UserList {

public static void main(String[] args) throws IOException {
	//declare variables
	String str1, str2 = "username";
	int index;
	int initialCapacity = 5;
	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
	ArrayList users = new ArrayList();
	
	//get user input
	System.out.print("Enter a user name: ");
	str1 = dataIn.readLine();
	
	//compare user input
	while(str1.length() > 1) { //ends when only enter is pressed
		//check for "username"
		if(str1 == str2)
			System.out.println("That user name is NOT allowed!");
		else {
			//check for capacity of 5
			if(users.size() == initialCapacity) {
				System.out.println("List is full!");
				//okay I got it to work but not able to list the last user just all of them
				System.out.println("Last entry is " +users);
			}
			else//check if user is in list already
				if(!users.contains(str1)) {
				users.add(str1);
				System.out.println("User \""+str1+"\" added to user list.");
				}
			else//
			System.out.println("User \""+str1+"\" already in user list.");
		}
		
		System.out.print("\nEnter a user name: ");
		str1 = dataIn.readLine();
	}
	//completion of program
	System.out.println("Program complete.");
	for(int i = 0 ; i < users.size(); i++) {
		System.out.println(users.get(i));
	}
	}
}




more help? keep posting.! :)

This post has been edited by letthecolorsrumble: 25 March 2008 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 01:00 PM

This code works just fine for me. What line does it say you have a problem?
Was This Post Helpful? 0
  • +
  • -

#5 JamesAngie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-February 08

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 01:17 PM

View PostGWatt, on 25 Mar, 2008 - 01:00 PM, said:

This code works just fine for me. What line does it say you have a problem?


It works fine...but I need it to only list the last user

in line 34

System.out.println("Last entry is " +users);

I don't know how to make it list ony the last person I entered. Currently it lists all the names I enter.
Was This Post Helpful? 0
  • +
  • -

#6 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 01:23 PM

users.get(4));



http://java.sun.com/.../ArrayList.html
Was This Post Helpful? 0
  • +
  • -

#7 JamesAngie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-February 08

Re: Index Out Of Bounds Exception

Posted 25 March 2008 - 01:29 PM

View Postcapty99, on 25 Mar, 2008 - 01:23 PM, said:




Thanks a million. I was adding [5] instead of (4)...
Was This Post Helpful? 0
  • +
  • -

#8 KoryLovre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-September 09

Re: Index Out Of Bounds Exception

Posted 03 September 2009 - 09:55 AM

Yes everyone - please remember that Java indexing is zero (0) based - this means you start counting at zero instead of 1. So if the total number of elements in your array is 5 then you index the 5 array elements as such:

first element index = 0 (users.get(0);
second element index = 1 (users.get(1);
third element index = 2 (users.get(2);
fourth element index = 3 (users.get(3);
fifth (last) element index = 4 (users.get(4);

Thus, trying to refer to an array with an index of any negative number or an index equal to or greater than 5 in this case will throw an IndexOutOfBoundsException as James was seeing.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Jamison Bryant*


Reputation:

Re: Index Out Of Bounds Exception

Posted 19 April 2010 - 02:36 PM

Here's what you do:

users.get(users.size() - 1);


This fetches the last entry in the ArrayList.
Was This Post Helpful? 0

Page 1 of 1