Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2011, 01:21 PM   PM User | #1
RandomRobbo
New to the CF scene

 
Join Date: Jul 2009
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
RandomRobbo is an unknown quantity at this point
Question 2D array search

Hi all,

Any help?

Im not completely sure if this is correct, I am new to java and want to input kitten and get cat for example on this 2d array. I get the exception ArrayIndexOutOfBounds.

Code:
public class Farm
import java.util.Arrays;
{

// Class variables
  	
 	public static String[][] animals =
{
{"kitten", "cat"},
{"puppy", "dog"},
{"lamb", "sheep"},
{"chick", "bird"},
{"calf", "cow"},
};


public String ChangefromAnimal1toAnimal2(String aString)
{
for (int r=0; r < Farm.animals.length; r++)
{
int index = Arrays.binarySearch(Farm.animals[r], aString);
{
  	
 	for (int c=0; c < Farm.animals[r].length; c++)
{
System.out.println(Farm.animals[r][index]);
}

}

}
}

}
I think am sorted!

Thanks,

Random

Last edited by Fou-Lu; 03-06-2011 at 07:43 PM.. Reason: Sorted
RandomRobbo is offline   Reply With Quote
Old 03-06-2011, 01:54 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,649
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
BinarySearch returns -1 for an index on failure. If you search for "kitten" and hit the second array for "puppy" there is no resulting match. Work wise, this is easier using a single loop.
PHP Code:
    public String ChangefromAnimal1toAnimal2(String aString)
    {
        
String sResult "";
        for (
int r 0Farm.animals.length && sResult.isEmpty(); r++)
        {
            if (
animals[r][0].equals(aString))
            {
                
sResult animals[r][1];
            }
        }
        return 
sResult;
    } 
Also consider using a Hashtable to associate "kitten" with "cat" for easier lookups.

Edit:
BTW, you'd never get the OutOfBounds exception. There are two compile time errors here, one for the import statement, and one for not returning a String from your method.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 03-06-2011, 02:21 PM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by RandomRobbo View Post
Hi all,

Any help?

Im not completely sure if this is correct, I am new to java and want to input kitten and get cat for example on this 2d array. I get the exception ArrayIndexOutOfBounds.


I think am sorted!

Thanks,

Random
I think you removed the content and this against forum rules. Put him back please.

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:42 PM.


Advertisement
Log in to turn off these ads.