Skip to content
Advertisement

If else condition with StringBuffer

I’m trying to make a If else condition to check if a current data exists on the database using a JSON request on a java application consuming a Rest Webservice.

So, I want to receive a boolean or make a condition to verify what i’m receiving (normally, null or true/false)

There’s a way to transform this in a boolean to make the condition:

        if (response.equals(true)){
//do action
    }

or how can I receive a StringBuffer and compare in this If-Else?

Here’s the code of how I’m receiving the StringBuffer:

public String sendGet(String url, String method) throws Exception {

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod(method);

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);

    responseCode = con.getResponseCode();
    System.out.println("nSending " + (method) + " request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    StringBuffer response;
    try (BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()))) {
        String inputLine;
        response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    }
    conecta.desconecta();
    return response.toString();

}

and here’s how i’m trying to make the condition:

                if  ((ConsumirWS2.responseCode == 200) || (ConsumirWS2.responseCode == 204) || (ConsumirWS2.response.equals(true))){
                
                JOptionPane.showMessageDialog(rootPane, "ExcluĂ­do com sucesso!"); //saying to the user "successfully deleted"
                
                jFormattedTextFieldCPF.setText("");
                jTextFieldNOME.setText(""); //deixa o campo vazio
                jFormattedTextFieldDATA.setText("");//deixa o campo vazio
                jTextFieldAPELIDO.setText(""); //deixa o campo vazio
                
                jFormattedTextFieldCPF.setEnabled(false); //deixa o campo indisponivel
                jTextFieldNOME.setEnabled(false);
                jFormattedTextFieldDATA.setEnabled(false);
                jTextFieldAPELIDO.setEnabled(false);
                jButtonINSERIR.setEnabled(true);
                jButtonALTERAR.setEnabled(false);
                
            }
            
            else{
                JOptionPane.showMessageDialog(rootPane, "Erro ao deletar!");//"Error during deletion process"
                
                jFormattedTextFieldCPF.setText("");
                jTextFieldNOME.setText(""); //deixa o campo vazio
                jFormattedTextFieldDATA.setText("");//deixa o campo vazio
                jTextFieldAPELIDO.setText(""); //deixa o campo vazio
                
                jFormattedTextFieldCPF.setEnabled(false); //deixa o campo indisponivel
                jTextFieldNOME.setEnabled(false);
                jFormattedTextFieldDATA.setEnabled(false);
                jTextFieldAPELIDO.setEnabled(false);
                jButtonINSERIR.setEnabled(true);
                jButtonALTERAR.setEnabled(false);
            }

Advertisement

Answer

A StringBuffer (or String) will never equal a boolean. But it could equal "true". Also you should prefer StringBuilder to StringBuffer. Assuming you have response as a StringBuilder (or a StringBuffer) you could do

if (response.toString().equalsIgnoreCase("true"))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement