如何将参数值从string转换为I. [英] How to convert get parameter values from string to I

查看:133
本文介绍了如何将参数值从string转换为I.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是;尝试将get参数字符串转换为int。我键入错误

The problem which I am having is; trying to convert get parameter string to int. I key getting the error "

Cannot invoke trim() on the primitive type int

上调用trim()不确定问题是什么,有人可以帮助请。



我得到rrror的行;

" not sure what the problem is can someone help please.

The line where I am getting the rrror on;

id.trim().equals(123)))





我尝试过:



我查看了stackoverflow并将get参数值转换为int。

错误就行了;



What I have tried:

I have looked on stackoverflow and converted the get parameter value to int.
Error is on the line;

return((id == null) || (id.trim().equals(123)));





请参阅下面的代码;



See the code below;

<pre>	// Creating protected int to get the id's
	protected int[] getIDs(HttpServletRequest request) {
		//		int[] ids = { 1, 2};
		//		String[] ids = { "missing", "missing" };
		// Creating array of int id's
		int[] ids = { 1};
		// Creating foor loop for the length of the id's
		for(int i=0; i<ids.length; i++) {
			//			int id = Integer.parseInt(id);
			//			int id = request.getParameterValues(123 + (i+1));
			// Converting get parameter string to integer using integer.getInteger
			int id = Integer.getInteger(request.getParameter("1234")).intValue();
			// Creating if statement for !isEmpty for id
			if (!isEmpty(id)) { ids[i] = id; }
		} // Close for loop
		// Return the id's
		return(ids);
	} // Close protected int array for getID's

	// Creating private boolean for is empty for the id, when user inputs the id, it will then return the data for that one film id.
	private boolean isEmpty(int id) {
		return((id == null) || (id.trim().equals(123)));
	}

推荐答案

你期望Trim函数做一个整数值是什么?它根本不能包含任何空格,所以实际上没有任何东西可以做!



Trim是一个基于字符串的函数:它从中删除空格字符串的开头和结尾。你不能将它应用于非基于字符串的值!



BTW:从什么时候开始,值123表示空整数?
What would you expect a Trim function to do an an integer value? It can't contain any whitespace at all, so there really isn't anything for it to do!

Trim is exclusively a string based function: it removes whitespace from the start and end of the string. You cannot apply it to non-string based values!

BTW: Since when did the value 123 indicate an "empty" integer anyway?

这篇关于如何将参数值从string转换为I.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆