In Internet programming, programmers receive parameters via a query string, whic
ID: 3684249 • Letter: I
Question
In Internet programming, programmers receive parameters via a query string, which looks like a String with fields separated by the "&" character. Each field typically has metadata part and a data part separated by the equal sign. An example of a query string is : first=Mike&last=Jones&id=mike1&password=hello
Using Scanner at least once, parse a query string like that above and output each field on a different line,
replacing the = sign with a colon followed by a space. For example, the output for the preceding sample
query string should look like:
first: Mike
last: Jones
id: mike1
password: hello
You can add more difficulty by putting the query string in a file and read it from the file. It is also
acceptable to simple create a variable called queryString and intialize it to a string similar to the
example given above.
Explanation / Answer
Answer:
import java.util.*;
public class ProgramStringrAndSplit {
public static void main(String[] args) {
String delimiter_taken = "&";
String split ="first=Mike&last=Jones&id=mike1&password=hello";
System.out.println("StringTokenizer Example: ");
StringTokenizer st = new StringTokenizer(split,delimiter_taken);
while (st.hasMoreElements()) {
System.out.println("Elements Taken: " + st.nextElement ());
}
String[] elements = split.split(delimiter_taken);
int elementCount = elements.length;
for (int j = 0; j < elementCount; j++) {
System.out.println(elements[j]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.