【问题标题】:How to a compare a string array variables to a normal string variable如何将字符串数组变量与普通字符串变量进行比较
【发布时间】:2017-04-08 08:35:30
【问题描述】:

我有两个表 Cart_table 和 product_table。我想做的是在购物车表中显示最受欢迎的商品,然后将其与我的产品表匹配并显示在我的页面上 我遇到的问题是变量的匹配

 String c="",pna="",pty="",ppr="",stock="",imgpath="";
                //String myList = new String[10];
                int a = 0;
                int result = 0,count = 0;
                int setres;
                int[] arr = new int[100000];

                  int item_id=0;
       data dt=new data();
       String item="",cartid="",user="";
       String[] prdid = new String[60];
       String[] comp = new String[60];
       int[] pr_id=new int[50];
       //item = "123";

       try{
           dt.st=dt.cn.createStatement();
                    //String select="select cartid,user_regid,user_prod_id,quantity from cart_table";
                    //String select="select cartid,user_regid,user_prod_id,quantity from cart_table";
                    /*String select="select "+
                            "cart_table.cartid,cart_table.user_regid,cart_table.user_prod_id,cart_table.quantity,cart_table.add_date,"+
                            "customer_table.fname,customer_table.email "+
                            "from cart_table "+
                            "inner join customer_table "+
                            "on cart_table.user_regid=customer_table.regid";*/
                    //String select="select * from cart_table";
                    String select_match="SELECT user_prod_id, COUNT(*) AS rep "+
                                        "FROM cart_table "+
                                        "GROUP BY user_prod_id "+
                                        "ORDER BY rep desc";
                    dt.rs=dt.st.executeQuery(select_match);

                        while(dt.rs.next()){



                                //String cid=dt.rs.getString("cartid");
                                //String prid=dt.rs.getString("user_prod_id");
                                prdid[a] = dt.rs.getString("user_prod_id");
                                pr_id[a] = dt.rs.getInt("user_prod_id");
                                String repp = dt.rs.getString("rep");
                                        //String date_add = dt.rs.getString("add_date");

                                out.println("This is int"+pr_id[a]);
                                out.println("<br/>"+prdid[a]);
                                out.println(repp);
                                a=a+1;
                                       }
                        out.println("<br/>---------xxx--------");
                        a=0;

                        String select3="select "+
                            "product_table.p_id,product_table.p_type,"+
                            "product_type.pt_id,"+
                            "product_table.p_name,product_table.imgpath,product_table.p_price,product_table.stock,product_table.add_date,"+
                            "product_type.pt_name "+
                            "from product_table "+
                            "inner join product_type "+
                            "on product_table.p_type=product_type.pt_id "+
                            "order by product_table.add_date desc"+
                            "";

                        dt.rs=dt.st.executeQuery(select3);
                    a = 0;
                        while(dt.rs.next()){

                                item_id=dt.rs.getInt("p_id");
                                String itemm=dt.rs.getString("p_id");
                                comp[a] = dt.rs.getString("p_id");
                                String com = dt.rs.getString("p_id");
                                pna=dt.rs.getString("p_name");
                                pty=dt.rs.getString("pt_name");
                                ppr=dt.rs.getString("p_price");
                                stock=dt.rs.getString("stock");
                                imgpath=dt.rs.getString("imgpath");
                                //String comp = prdid[a];
                                //out.println("<br/>a"+comp+"This is a compare value<br/>");
                                //out.println("<br/>a"+prdid[a]+"This is an actual value<br/>");
                                //if(pr_id[a] == item_id)
                                //if(Arrays.asList(pr_id).contains(item_id))

> 

                                if(prdid[a].equals(com))
                                {




                        %>
                        <li class="span3">
                                <div class="product-box">
                                    <span class="sale_tag"></span>                                              
                                                                        <a href="product_detail.jsp?prdid=<%=item_id%>"><img alt="" class="imdis" src="<% out.println(imgpath); %>"></a><br/>
                                    <a href="product_detail.jsp?prdid=<%=item_id%>" class="title"><% out.println(pna); %></a><br/>
                                    <a href="#" class="category"><% out.println(pty); %></a>
                                                                        <p> <% out.println(item_id); %></p>
                                    <p class="price"><% out.println(ppr); %></p>
                                </div>
                            </li>       `


                        <%
                                }
                                 a = a+1;
}





    }
    catch(Exception ex){
        out.println(ex);
    }

我的主要问题是这段代码

 if(prdid[a].equals(com))
                                {




                        %>
                        <li class="span3">
                                <div class="product-box">
                                    <span class="sale_tag"></span>                                              
                                                                        <a href="product_detail.jsp?prdid=<%=item_id%>"><img alt="" class="imdis" src="<% out.println(imgpath); %>"></a><br/>
                                    <a href="product_detail.jsp?prdid=<%=item_id%>" class="title"><% out.println(pna); %></a><br/>
                                    <a href="#" class="category"><% out.println(pty); %></a>
                                                                        <p> <% out.println(item_id); %></p>
                                    <p class="price"><% out.println(ppr); %></p>
                                </div>
                            </li>       `


                        <%
                                }

【问题讨论】:

  • 请先整理一下代码
  • ... 并将其缩减为 minimal reproducible example。您可以在一个小型控制台应用程序中完全不使用 JSP 来演示问题。

标签: java arrays string jsp compare


【解决方案1】:

看起来很正常,如果你的编码没问题,它应该可以工作。 在比较之前尝试获取和比较每个字符串的字节:

byte[] utf8Bytes1 = firstString.getBytes("UTF-8");
byte[] utf8Bytes2 = secondString.getBytes("UTF-8");

然后打印这些数组并比较字节。 之后分享结果,可能字节不相等,因为DB编码什么的。

【讨论】:

  • 第一个字符串[B@39b6013d 第二个字符串[B@70d31029 第一个字符串[B@fd99c60 第二个字符串[B@468a5ecf 第一个字符串[B@4f3c00ca 第二个字符串[B@2706035c 第一个字符串[B@647fa140]第二个字符串[B@7017f726 第一个字符串[B@5f8293be 第二个字符串[B@488977f1 第一个字符串[B@5165ba01 第二个字符串[B@1e04d9cc 第一个字符串[B@55d0b6d1 第二个字符串[B@56657565 java.lang.NullPointerException
  • 作为输出我得到了这个
【解决方案2】:

不,我提到了这样的事情:

try {
                String firstString = "test1";
                    byte[] utf8Bytes1 = firstString.getBytes("UTF-8");
                String secondString = "test2";
                byte[] utf8Bytes2 = secondString.getBytes("UTF-8");
                for (int i = 0; i < utf8Bytes1.length; i++) {
                    System.out.println("from first string: " + utf8Bytes1[i]);
                }

                for (int i = 0; i < utf8Bytes2.length; i++) {
                    System.out.println("from second string: " + utf8Bytes2[i]);
                }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

【讨论】:

  • 我在 catch (UnsupportedEncodingException e) { e.printStackTrace(); }
  • 您的数据库和前端使用什么编码?语言?
猜你喜欢
  • 2015-10-03
  • 2018-03-17
  • 1970-01-01
  • 2016-03-03
  • 2014-11-08
  • 2018-08-23
  • 2020-03-26
  • 2012-04-06
  • 2013-01-17
相关资源
最近更新 更多