1. Engineering
  2. Computer Science
  3. java code problem level introduction to java write a method...

Question: java code problem level introduction to java write a method...

Question details

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:

some other key -1.4, the key we want: some value

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:

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.

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!

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution