Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code problem level: introduction to java Write a method as shown below: protected int findKey (String keyToFind, String stringObject) This method takes in keyToFind

java code problem

level: introduction to java

Write a method as shown below:

protected int findKey (String keyToFind, String stringObject)

This method takes in keyToFind (the key we are looking for) and the String stringObject (the object that you need to traverse to find the key). We assume that the input String stringObject always has a specific format. The first element of a stringObject is { and the last element is } . For example, "{12345}" has a expected input. "{1}, {2}, {345}, {6}" is a expected string input. The use of { and } separate the string into 4 objects, which are {1}, {2}, {345} and {6}.

The method findKey should return the index in stringObject of the ending quotation mark of the first key matching the given key to look for. If the key is not found in the String stringObject, return -1.

For example, searching for the_key_we_want in the stringObject below:

image text in transcribed

You would return the index of the ending quotation mark around the_key_we_want as shown above with the ^ pointing to it.

Second example:

String keyToFind ="the first value";

String stringObject = "{"the first value": 10}";

int output = findKey(keyToFind, stringObject);

===> output = 17

In the above example, the keyToFind, which is "the first value" ends at index 17 in the string. This is the index that we want to return.

Third example:

image text in transcribed

Remember that this example is simplified, so typically you would need to check more than one key before finding the correct one.

For example:

String keyToFind ="the first value";

String stringObject = "{"the first": 10, "the first value"}";

int output = findKey(keyToFind, stringObject);

===> output = 34

Thanks!

"some other key" -1.4, "the key we want": "some value" keyToFind: the temp 1 0 1 2 34 56 78 9 10 11 12 13 14 15 16 17 18 19 In the above example, the keyToFind (the_temp) ends at index 10 in the string. This is the index that we want to return. Remember that this example is simplified, so typically you would need to check more than one key before finding the correct one

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions

Question

b. What are its goals and objectives?

Answered: 1 week ago