ARRANGEMENT TYPE PROCESSING METHOD HAVING CHARACTER STRING TYPE IN ARGUMENT AND CHARACTER STRING TYPE IN

Asked 1 years ago, Updated 1 years ago, 34 views

I'd like to create a user-defined method.
I don't know what to do with the description of the caller of the method.
Also, I am not sure if the method definition is correct, so please let me know.

Details below…

·Argument → string type
·Return value → array type with string type as element
·Contents of processing
→ Create profile information for 3 people in each array.
An array having the same ID as the ID value received as the argument is returned as a return value.
After creating the method, call it and
Elements stored in the return value are respectively displayed.
At this time, elements to be displayed shall be specified in order by repetitive processing, and ID values shall not be displayed.

// Code

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

/** 

@authorotoja
*/
public class kadai extensions HttpServlet { 
/** 
Processes requests for both HTTP<code>GET>/code>and>POST>/code> 
methods. 

@param request servlet request 
@param response servlet response 
@throws ServletException if a servlet-specific error occurred 
@throws IOException if an I/O error occurred
*/ 
String [ ] profile (String key) {
    String [ ] [ ] profile_list = {
        {"ID: 123456", "Born November 8, 1993", "Nerima Ward, Tokyo",
        {"ID: 987654", "Born March 14, 2012", "Mitaka City, Tokyo",
        {"ID:192837", "Born June 17, 2017", "Nakano Ward, Tokyo"; 
    if(key.equals(profile_list[1])){
        return profile_list[1]; 
    } else if(key.equals(profile_list[0])) {
        return profile_list[0];
    } else {
        return profile_list[2];
    }
} 

Is the above correct?
Also, how should I describe the calling party after processRequest?
Thank you for your cooperation.

java

2022-09-30 21:32

2 Answers

Although 表示display 」 probably means displaying it as HTML, there is not enough detailed input and output specification (format) information to provide an example of how to display it in the standard output, one line at a time.

When you ask questions, be sure to include detailed information about your development and execution environment (OS, JDK version, etc.) as a minimum manner.Newer versions of the Java language specification have added features that allow you to write code more concisely, but not in older JDKs.DKs.As a result, environmental information affects how easy it is to answer.

pattern language

when asking questions in technical mailing lists

First, learn the grammar for indexing array (jug array).

If you want to linear search of elements from a collection such as an array, you can generalize them by using loops.It's easy to deal with cases where the number of elements is not determined at compile time and only known at runtime.Conditional branching makes code less maintainable when the number of elements increases.

However, the search time is O (n), which is linearly proportional to the number of elements.Typically, large databases are placed in memory using search-efficient maps or HashMap.

/**
 * Returns the profile that corresponds to the key.
 * @param key Search key.
 * @return The profile found.Returns null if not applicable.
 */
static String [ ] searchProfile (String key) {
    final String [ ] [ ] profileList = {
        {"ID: 123456", "Born November 8, 1993", "Nerima Ward, Tokyo",
        {"ID: 987654", "Born March 14, 2012", "Mitaka City, Tokyo",
        {"ID:192837", "Born June 17, 2017", "Nakano Ward, Tokyo",
    };
    // Linear search. Search time is linearly proportional to the number of elements.
    for (inti=0;i<profileList.length;++i){
        if(key.equals(profileList[i][0])){
            return profileList [i];
        }
    }
    return null;// Not applicable.
}

public static void main(String[]args) rows java.lang.Exception {
    String[] profile=searchProfile("ID:123456");
    if(profile==null){
        System.out.println("Not found.");
        return;
    }
    for (inti=1;i<profile.length;++i){
        System.out.println(profile[i]);
    }
}

By the way, who is "processRequest" in the questionnaire?
We assume that you are referring to a user-defined method called within HttpServlet.doGet(), HttpServlet.doPost(), but how you describe the contents of the method depends on what servlet you implement:Override doGet() if you only want to print HTML.The Servlet implementation will deviate from the title (the subject of this question), so please separate the topics without mixing the questions, or correct and add the questions themselves appropriately.

public class MyServlet extensions HttpServlet{

    protected void processRequest(HttpServletRequest, HttpServletResponse response)throwsServletException, IOException {
        // TODO—Implemented processing on client request.
    }

    @ Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)rowsServletException, IOException {
        processRequest (request, response);
    }

    @ Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)rowsServletException, IOException {
        processRequest (request, response);
    }
}

Also, in Java custom, start the class name with a capital letter, and learn about Javadoc comments.


2022-09-30 21:32

if(key.equals(profile_list[1])){
    return profile_list[1]; 
} else if(key.equals(profile_list[0])) {
    return profile_list[0];
} else {
    return profile_list[2];
}

This if statement always returns profile_list[2] because it compares the key of type String with the profile_list[1]/profile_list[0] of type String[].

If I were to simply implement it, would it be as follows?

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @authorotoja
 */
public class kadai extensions HttpServlet {

    @ Override
    protected void doGet (HttpServletRequest request, HttpServletResponse response)
            US>throws ServletException, IOException {
        This.processRequest(request, response);
    }

    @ Override
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            US>throws ServletException, IOException {
        This.processRequest(request, response);
    }

    /**
     * Processes requests for both HTTP<code>GET>/code>and>POST>/code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurred
     * @throws IOException if an I/O error occurred
     */
    protected void processRequest (HttpServletRequest, HttpServletResponse response)
            US>throws ServletException, IOException {
        String id = request.getParameter("id");
        if(id==null){
            return;
        }
        String [ ] profile = this.profile(id);
        if(profile!=null){
            response.addHeader("Content-Type", "text/plain; charset=UTF-8";
            for(inti=1;i<profile.length;i++){
                response.getWriter().println(profile[i]);
            }
        }
    }

    String [ ] profile (String key) {
        String [ ] [ ] profile_list = {
                { "ID: 123456", "Born November 8, 1993", "Nerima Ward, Tokyo",
                { "ID: 987654", "Born March 14, 2012", "Mitaka City, Tokyo"},
                { "ID: 192837", "Born June 17, 2017", "Nakano Ward, Tokyo"}
        };
        for(String[]profile:profile_list){
            if(profile[0].equals(key)){
                return profile;
            }
        }
        return null;
    }
}


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.