简体   繁体   English

整数 - Java 中的字符串转换问题

[英]Integer - String conversion issue in Java

What is the best way to convert String to Integer.将字符串转换为整数的最佳方法是什么。

params.get("contractPriceId"); here I am getting Exception cannot cast Integer to String...在这里我得到 Exception cannot cast Integer to String ...

public List<ContractPriceDetailVO> getContractPriceDetails(
        Map<String, String> params) throws DAOException {
    // Get the request object
    HttpServletRequest request = FlexContext.getHttpRequest();
    // Get the AppContext object
    AppContext ac = SessionUtils.getAppContext(request);
    SimpleDateFormat sdf = new SimpleDateFormat(ac.getDateFormat());
    List<Object[]> resultList = new ArrayList<Object[]>();
    if (params.get("specId") != null && params.get("specId").trim() != ""
            && params.get("effDate") != null
            && params.get("effDate").trim() != null)
        resultList = contractPriceEJBService.getContractPriceDetails(ac,
                params);

    String contractPriceId = null;
    if (params.get("contractPriceId") != null){
        try{
        contractPriceId = params.get("contractPriceId");
        }
        catch (Exception e){
            logger.error("Exception in getting contractPriceId "+e.getMessage());
        }
    }
    else{
        contractPriceId = null;
    }

Which exception are you getting ?你得到哪个例外?

Normally we use,通常我们使用,

int price = Integer.parseInt("7");

This returns the int value of string.这将返回字符串的 int 值。

Please put the exception and error in the question so we can do more analysis of issue.请将异常和错误放在问题中,以便我们对问题进行更多分析。

像这样改变它,它应该可以工作

contractPriceId = params.get("contractPriceId") +"";

From Integer to String, String.valueOf(47).从整数到字符串,String.valueOf(47)。

From String to Integer, Integer.parseInt("47").从字符串到整数,Integer.parseInt("47")。

To avoid the exception, try String.valueOf(params.get("contractPriceId")).为避免异常,请尝试 String.valueOf(params.get("contractPriceId"))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM