If, else if conditions in java

2

I'm new to this Java topic and I run into a problem, I have a form where gender reports PDF in which I have several options to generate them, full report of the table, detailed report by date, report with just a search using SQL LIKE statements and reports by columns of the table, well finally I can generate the reports and queries, the problem is that I only have one button to generate the 4 reports in different ways, depending on the data you enter in the boxes text (String, all data is of this type).

How do I make the condition to select the appropriate report depending on what you enter in the text boxes?

This is the condition I use

String a = funciones.getFecha(fechad);
String b = funciones.getFecha(fechaa);
String c = txtbusqueda.getText();

if ((a != null) && (!a.equals("")) && (b != null) && (!b.equals("")) && (c!=null) && (!c.equals(""))) {
mi reporte 1
}
else if ((c != null) && (!c.equals(""))) {
    reporte2
}
else{
    reporte3
}

I just want to evaluate if the variables contain values or not but I do not know how: '(

    
asked by Fabian Villaseñor 26.11.2018 в 14:02
source

3 answers

1

From what I have understood in your question you could solve it more or less like this:

 String a = funciones.getFecha(fechad);
    String b = funciones.getFecha(fechaa);
    String c = txtbusqueda.getText();

    if (hasValue(c)){
        if (hasValue(a) && hasValue(b)){
            reporteQuerySQLConFechas;
        }else{
            reporteQuerySQL;
        }
    }else{
        if (hasValue(a) || hasValue(b)){
            reporteFechas;
        }else{
            reporteTabla;
        }
    }

And the hasValue function would create it that way so that the code is much clearer.

private boolean hasValue(String myString){
    return (myString != null) && (!myString.equals(""));
}
    
answered by 26.11.2018 в 14:31
0
Ya lo resolví, muchas gracias a todo pos su apoyo.

:)

private void btnPDFActionPerformed(java.awt.event.ActionEvent evt) {                                       
    String a = funciones.getFecha(fechad);
    String b = funciones.getFecha(fechaa);
    String e = txtbusqueda.getText();

    try {

        if (e.isEmpty()) {
         if (b==null && a==null)  {
                JasperReport reporte = null;
                reporte = null;
                String path = "src\Report\reportv.jasper";

                reporte = (JasperReport) JRLoader.loadObjectFromFile(path);

                JasperPrint jprint = JasperFillManager.fillReport(reporte, null, cc);

                JasperViewer view = new JasperViewer(jprint, false);

                view.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

                view.setVisible(true);


            } else {
                JasperReport reporte = null;
                reporte = null;
                String path = "src\Report\refortfacha.jasper";
                Map parametro = new HashMap();
                parametro.put("fechadesde", a);
                parametro.put("fechahatsa", b);

                reporte = (JasperReport) JRLoader.loadObjectFromFile(path);

                JasperPrint jprint = JasperFillManager.fillReport(reporte, parametro, cc);

                JasperViewer view = new JasperViewer(jprint, false);

                view.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

                view.setVisible(true);

            }

        }  else {
            if(!e.isEmpty()) {
                if (b==null && a==null){
            JasperReport reporte = null;
            reporte = null;
            String path = "src\Report\ReportIndividual.jasper";
            Map parametro = new HashMap();
            parametro.put("valor", e);

            reporte = (JasperReport) JRLoader.loadObjectFromFile(path);

            JasperPrint jprint = JasperFillManager.fillReport(reporte, parametro, cc);

            JasperViewer view = new JasperViewer(jprint, false);

            view.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

            view.setVisible(true);
            }else{
                    JasperReport reporte = null;
                reporte = null;
                String path = "src\Report\refortfacha.jasper";
                Map parametro = new HashMap();
                parametro.put("fechadesde", a);
                parametro.put("fechahatsa", b);

                reporte = (JasperReport) JRLoader.loadObjectFromFile(path);

                JasperPrint jprint = JasperFillManager.fillReport(reporte, parametro, cc);

                JasperViewer view = new JasperViewer(jprint, false);

                view.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

                view.setVisible(true);
                }

            }
        } 


        // >:v

    } catch (JRException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }


} 
    
answered by 26.11.2018 в 15:41
0

In java the operator "==" tests whether the two operands refer to the same object, ie:

String x = "HOLA";
String y = new String(new char[] { 'H', 'O', 'L', 'A'});

Evaluation (x == y) -> False

I recommend using the methods contains and equals . equals evaluates if there is an equity, that is, if they are "equal"

String x = "HOLA";
String y = new String(new char[] { 'H', 'O', 'L', 'A'});

Evaluation (x.equals (y)) -> True

I recommend you read: link

In your example:

string MiResultado = funciones.getFecha(fechad);

If I want the REPORT 1 report to be generated when MyResult is "RESULT1"

if(MiResultado.equals("RESULTADO1"))
{
  REPORTE1();
}

If I want REPORT2 to be generated when MyResult contains "RESULT2"

if(MiResultado.contains("RESULTADO2 y también el RESULTADO3"))
{
  REPORTE3();
}

Keep in mind that adding if but not else will evaluate both cases. If you want the case to be unique add else before the second if

    
answered by 26.11.2018 в 14:29