I have some code but I need it to be as a RESTful service, which I can post to A
ID: 3793777 • Letter: I
Question
I have some code but I need it to be as a RESTful service, which I can post to Amazon web services
import java.util.*;
public class JSON_Test{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("** Enter input in Single line **");
String input = sc.nextLine();
//System.out.println(input);
StringBuffer name = new StringBuffer();
StringBuffer list_str = new StringBuffer();
int[] original;
int name_in=0;
boolean name_flag = false;
int i = 0;
int len = input.length();
while(i<len && input.charAt(i) == ' '){
i++;
}
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
if(input.charAt(i) == '{'){
i++;
while( i<len && input.charAt(i) == ' '){
i++;
}
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
if(input.charAt(i) == '"'){
i++;
while(i<len && input.charAt(i) != '"'){
name.append(input.charAt(i));
i++;
}
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
i++;
while( i<len && input.charAt(i) == ' '){
i++;
}
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
if(input.charAt(i) ==':'){
i++;
while( i<len && input.charAt(i) == ' '){
i++;
}
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
if(input.charAt(i)=='['){
i++;
if(i==len){
System.out.println("Incorrect input !!");
System.exit(0);
}
while(i<len && input.charAt(i) != ']'){
list_str.append(input.charAt(i));
i++;
}
StringTokenizer st = new StringTokenizer(list_str.toString(),",");
original = new int[st.countTokens()];
while (st.hasMoreTokens()) {
try{
original[name_in] = Integer.parseInt(st.nextToken());
name_in++;
}
catch(Exception e){
System.out.println("Incorrect Input. Hint : Can't find "numbers" in the input");
System.exit(0);
}
}
i++;
while( i<len && input.charAt(i) == ' '){
i++;
}
if( i >= len || input.charAt(i) != '}' ){
System.out.println("Incorrect Input. Hint : Closing Tag");
System.exit(0);
}
Merge mms = new Merge();
long startTime = System.currentTimeMillis();
mms.sort(original);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.print("{ "outList" :[");
for(int j=0; j<original.length;j++){
System.out.print(original[j]);
if(j != original.length-1){
System.out.print(",");
}
}
System.out.print("], "algorithm" : "Mergesort", ");
System.out.print(" "timeMs" : "+ elapsedTime +" } ");
}
}
}
else{
System.out.println("Incorrect Input. Hint : Can't find "inlist" Starting quote ' " ' ");
System.exit(0);
}
}
else{
System.out.println("Incorrect Input. Hint : Can't find "JSON list" Starting brace - "{" ");
System.exit(0);
}
}
}
class Merge{
private int[] array;
private int[] tempArr;
private int length;
Merge(){
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempArr = new int[length];
MergeSort_Partition(0, length - 1);
}
private void MergeSort_Partition(int low, int high) {
if (low < high) {
int middle = low + (high - low) / 2;
MergeSort_Partition(low, middle);
MergeSort_Partition(middle + 1, high);
sortwith_Parts(low, middle, high);
}
}
private void sortwith_Parts(int low, int middle, int high) {
for (int i = low; i <= high; i++) {
tempArr[i] = array[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}
Explanation / Answer
package InsertionSort; // Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.json.*; import java.util.List; import java.util.Arrays; import java.util.*; // Extend HttpServlet class public class ReverseList extends HttpServlet { // Standard servlet method public void init() throws ServletException { // Do any required initialization here - likely none } // Standard servlet method - we will handle a POST operation public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doService(request, response); } // Standard servlet method - we will not respond to GET public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type and return an error message response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.println("{ 'message' : 'Use POST!'}"); } // Our main worker method // Parses messages e.g. {"inList" : [5, 32, 3, 12]} // Returns the list reversed. private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get received JSON data from HTTP request BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); String jsonStr = ""; if(br != null){ jsonStr = br.readLine(); } // Create JsonReader object StringReader strReader = new StringReader(jsonStr); JsonReader reader = Json.createReader(strReader); // Get the singular JSON object (name:value pair) in this message. JsonObject obj = reader.readObject(); //Json object that holds the output of the sort JsonArrayBuilder outArrayBuilder = Json.createArrayBuilder(); //From the object get the array named "inList" JsonArray inArray = obj.getJsonArray("inList"); int arrLen = inArray.size(); int[] arr = new int[arrLen]; int temp; long sortBegin = 0; long sortEnd = 0; long timeSorted = 0; String errorCheckString = inArray.toString(); char c; boolean validJson = false; int strLen = errorCheckString.length(); for (int t = 0; t = '0' && cRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.