如何将字符串数组变量与普通字符串变量进行比较



我有两个表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>       `

                        <%
                                }

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

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

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

不,我有这样的想法:

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();
                }

相关内容

  • 没有找到相关文章

最新更新