The Artima Developer Community
Sponsored Link

Java Answers Forum
Arrays

2 replies on 1 page. Most recent reply: Nov 7, 2005 1:42 AM by Marius Jursys

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Jaelyn

Posts: 6
Nickname: tarheel
Registered: Sep, 2005

Arrays Posted: Nov 5, 2005 5:13 PM
Reply to this message Reply
Advertisement
I am writing a program that contains apartment rooms. The program is supposed to determine the level of noise and pollution in a given room that is due to a combination of the room tenant's own behavior and the neighbors several rooms to the left and right. My problem is that the array index is out of bounds when the program searches for the room number. I need help on having the array index match up with the array with the room information. Here is my code:

import java.util.Scanner;
public class Program04
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
System.out.println("Tenants: ");
System.out.println("Jack, Jane, John, Spot, Apple, Bill, George, Steve, Anne, Meow");

System.out.println();

System.out.print("Please enter the room number to test: ");
int Tenant;
Tenant = console.nextInt();

System.out.print("Please enter the neighborhood radius: ");
int Radius;
Radius = console.nextInt();


ApartmentRoom[] Room = {new ApartmentRoom(21, "Jack", 500, 80, 20),
new ApartmentRoom(22, "Jane", 450, 10, 10),
new ApartmentRoom(23, "John", 600, 5, 50),
new ApartmentRoom(24, "Spot", 200, 100, 100),
new ApartmentRoom(25, "Apple", 150, 1, 1),
new ApartmentRoom(26, "Bill", 520, 30, 25),
new ApartmentRoom(27, "George", 480, 40, 5),
new ApartmentRoom(28, "Steve", 800, 70, 55),
new ApartmentRoom(29, "Anne", 750, 10, 10),
new ApartmentRoom(30, "Meow", 450, 90, 10)};
int num;
for(int j = 0; j < Room.length; j++)
{
ApartmentRoom w = Room[j];
int y = w.getRoomNum();

if(Tenant == y )
{
num = j;

Room[num].print();

}

for(int i=0; i <= Radius; i++)
{

int pollute = Room.getPollutionLevel();
int noise = Room.getNoiseLevel();

System.out.println(pollute);
System.out.println(noise);
}

for(int count=0; count<=Radius; count++)
{


Room[y-count].getPollutionLevel();
Room[y+count].getPollutionLevel();

Room[y-count].getNoiseLevel();
Room[y+count].getNoiseLevel();
}
}
}
}


Paul de Vrieze

Posts: 9
Nickname: pauldv
Registered: Oct, 2005

Re: Arrays Posted: Nov 7, 2005 1:34 AM
Reply to this message Reply
You should make sure that roomnumber +- i (where i is all possible radii) is allways inside the array range. Remember that room[0] does only have a neighbor on one side.

Marius Jursys

Posts: 3
Nickname: shadow3d
Registered: Nov, 2005

Re: Arrays Posted: Nov 7, 2005 1:42 AM
Reply to this message Reply
I think you geting error on this part of code:

for(int count=0; count<=Radius; count++){
Room[y-count].getPollutionLevel();
Room[y+count].getPollutionLevel();

Room[y-count].getNoiseLevel();
Room[y+count].getNoiseLevel();
}

because earlear y was equal to roomNumber, lets say 22
and you created only 10 rooms array.

if you want to use such statment you must create room = new ApartmentRoom(i,"Jane", 450, 10, 10) - sa that index of array and room number match.

or you could write short method like:

ApartmentRoom getRoom(int nr){
for(int j = 0; j < Room.length; j++){
if(nr == Room[j].getRoomNum())
return Room[j];
}
}

when you can call it:

for(int count=0; count<=Radius; count++){
ApartmentRoom a, b;
a = getRoom(y-count);
b = getRoom(y+count);
a.getPollutionLevel();
b.getPollutionLevel();

a.getNoiseLeve l();
b.getNoiseLevel();
}

Flat View: This topic has 2 replies on 1 page
Topic: Problem to Connect JMXConnectorServer Previous Topic   Next Topic Topic: Linking external jar while launching a jar problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use