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 String extractValue (int index, String jsonObject)

This method takes in the index of the ending quotation mark of a key (before the colon) in the jsonObject and returns the value (after the colon) for that key as a String. Make sure to check if the input index is a valid index. If it is an invalid index, return null.

For example, given the jsonObject below:

{"some_other_key": -1.4, "the_key_we_want": "some value"}
  • If index pointed to the ending quotation mark around "some_other_key", you would return the String -1.4
  • If index pointed to the ending quotation mark around "the_key_we_want", you would return the String some value

Reminder: There shouldn't be any whitespace or any other trailing characters (like quotation marks or colon) at the ends of value.Example of extractValue: keyToFind: the_temp By looking at the given index (10), you can find the value need to extract by checking the next characters. Once vou trim it to the String 27.670, return it you t h e e m 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Trim to 27.670

Hints: 1. find the index of ":" which is nearest to the input index. And the index of the ":" should come after the input index

2. Record everything that comes after that until you hit "," or "}".

3. return whatever you have recorded

4. If the input jsonObject is {"the_key_we_want":    10}

5. JSON object library should not be used. Treat the @param jsonObject as a normal string.

the output will be "10" without extra space.

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