String to int conversion
can n e body tell me how can i convert a String to an int
always remember to use the try/catch block with thins that throw exceptions
String gg;
int in;
try{
gg = "30";
in = Integer.parseInt(gg);
catch(NumberFormatException e){
e.printStackTrace();
}Ant
Similar Messages
-
Problem with String to Int conversion
Dear Friends,
Problem with String to Int conversion
I am having a column where most of the values are numeric. Only 4 values are non numeric.
I have replaces those non numeric values to numeric in order to maintain the data type.
CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END
This comes the result as down
Grade
_0_
_1_
_10_
_11_
_12_
_13_
_14_
_15_
_16_
_17_
_18_
_19_
_2_
_20_
_21_
_22_
_23_
_24_
_3_
_4_
_5_
_6_
_7_
_8_
_9_
Refresh
Now I want to convert this value to numeric and do some calculation
So I changed the formula as below
cast (CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END as INT)
Now I get the following error
View Display Error
_ Odbc driver returned an error (SQLExecDirectW)._
Error Details
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
_State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 17001] Oracle Error code: 1722, message: ORA-01722: invalid number at OCI call OCIStmtFetch. [nQSError: 17012] Bulk fetch failed. (HY000)_
SQL Issued: SELECT cast ( CASE Grade.Grade WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' ELSE Grade.Grade END as Int) saw0 FROM "Human Capital - Manpower Costing" WHERE LENGTH(CASE Grade.Grade WHEN 'E1' THEN '20' WHEN 'E2' THEN '21' WHEN 'E3' THEN '22' WHEN 'E4' THEN '23' ELSE Grade.Grade END) > 0 ORDER BY saw_0_
Refresh
Could anybody help me
Regards
Mustafa
Edited by: Musnet on Jun 29, 2010 5:42 AM
Edited by: Musnet on Jun 29, 2010 6:48 AMDear Kart,
This give me another hint, Yes you are right. There was one row which returns neither blank nor any value.
I have done the code like following and it works fine
Thanks again for your support
Regards
Code: cast (CASE (CASE WHEN Length(Grade.Grade)=0 THEN '--' ELSE Grade.Grade END) WHEN 'E4' THEN '24' WHEN 'E3' THEN '23' WHEN 'E2' THEN '22' WHEN 'E1' THEN '21' when '--' then '-1' ELSE Grade.Grade END as Int) -
Please help me with my code (has conversion from string to int)
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Test3 extends MIDlet implements CommandListener
{ Form mForm;
Command mCommandQuit;
Command mCommandItem;
TextField input,prime1,prime2,prime3,output;
Display mDisplay;
int i,j=0,k1,k2,p,q,b;
//int [] current=new int [1000];
String mstring,a="";
String [] temp=new String [1000];
public void startApp()
{System.out.println("startApp");
mForm=new Form("RSA Encryption");
mCommandQuit=new Command("QUIT",Command.EXIT,0);
mCommandItem=new Command("ENCRYPT",Command.ITEM,0);
mForm.append("Enter text:\n");
input=new TextField(null,"",100, TextField.ANY);
mForm.append(input);
mForm.append("Enter first prime number(p):\n");
prime1=new TextField(null,"",100, TextField.ANY);
mForm.append(prime1);
mForm.append("Enter second prime number(q):\n");
prime2=new TextField(null,"",100, TextField.ANY);
mForm.append(prime2);
mForm.append("Enter d:\n");
prime3=new TextField(null,"",100, TextField.ANY);
mForm.append(prime3);
mForm.addCommand(mCommandQuit);
mForm.addCommand(mCommandItem);
mDisplay=Display.getDisplay(this);
mDisplay.setCurrent(mForm);
mForm.setCommandListener(this);
public void pauseApp()
{System.out.println("pauseApp");
public void destroyApp(boolean unconditional)
{System.out.println("destroyApp");
public void commandAction(Command c, Displayable d)
{System.out.println("commandAction");
if(c==mCommandQuit)
notifyDestroyed();
else if(c==mCommandItem)
{p = Integer.parseInt(prime1.getString());
q = Integer.parseInt(prime2.getString());
b = Integer.parseInt(prime3.getString());
//breaking up of big string into ints
mstring=input.getString();
temp[0]="";
for(i=1;i<mstring.length();i++)
{if (mstring.charAt(i) == ' ')
{j++;
temp[j]="";
else
{temp[j]=temp[j]+mstring.charAt(i);
mForm.append("\n\nThe array is:\n");
for(i=0;i<temp.length && temp!=null;i++)
{k1=Integer.parseInt(temp[i]); ***********************
k2=k1;
for(j=1;j<b;j++)
{k1=k1*k2;
k1=k1 %(p*q);
k2=k1 %(p*q);
a=a+new Character((char)k2).toString();
output=new TextField(null,a,100, TextField.ANY);
mForm.append(output);
}hi
this code basically takes an input of string like " 179 84 48 48 155 " (with spaces)
then it creates smaller strings in an array like "179","84","48","48","155" without the spaces
then it creates int values 179,84,48,48,155 and finally after some math functions it prints the corresponding letters.
the problem is that it is not printing the letter because of some exceptions-->java.lang.NumberFormatException .it comes in the line where i have put stars
could anybody please help me print the lettersthanks for all ur help guys, but me and my team member solved it on our own. here is the new code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Test3 extends MIDlet implements CommandListener
{ Form mForm;
Command mCommandQuit;
Command mCommandItem;
TextField input,prime1,prime2,prime3,output;
Display mDisplay;
int i,j=0,l,k1,k2,p,q,n,b;
String mstring,a = "";
String [] temp=new String [1000];
public void startApp()
{System.out.println("startApp");
mForm=new Form("RSA Encryption");
mCommandQuit=new Command("QUIT",Command.EXIT,0);
mCommandItem=new Command("DECRYPT",Command.ITEM,0);
mForm.append("Enter text:\n");
input=new TextField(null,"",100, TextField.ANY);
mForm.append(input);
mForm.append("Enter first prime number(p):\n");
prime1=new TextField(null,"",100, TextField.NUMERIC);
mForm.append(prime1);
mForm.append("Enter second prime number(q):\n");
prime2=new TextField(null,"",100, TextField.NUMERIC);
mForm.append(prime2);
mForm.append("Enter d:\n");
prime3=new TextField(null,"",100, TextField.NUMERIC);
mForm.append(prime3);
mForm.addCommand(mCommandQuit);
mForm.addCommand(mCommandItem);
mDisplay=Display.getDisplay(this);
mDisplay.setCurrent(mForm);
mForm.setCommandListener(this);
public void pauseApp()
{System.out.println("pauseApp");
public void destroyApp(boolean unconditional)
{System.out.println("destroyApp");
public void commandAction(Command c, Displayable d)
{System.out.println("commandAction");
if(c==mCommandQuit)
notifyDestroyed();
else if(c==mCommandItem)
{p = Integer.parseInt(prime1.getString());
q = Integer.parseInt(prime2.getString());
b = Integer.parseInt(prime3.getString());
n=p*q;
//breaking up of big string into ints
mstring=input.getString();
temp[0]="";
for(i=1;i<mstring.length();i++)
{if (mstring.charAt(i) == ' ')
{j++;
temp[j]="";
else
{temp[j]=temp[j]+mstring.charAt(i);
l=j;
mForm.append("\n\nThe result is:\n");
for(i=0;i<l;i++)
{k1=Integer.valueOf(temp).intValue();
k2=k1;
for(j=1;j<b;j++)
{k2=k2*k1;
k2=k2 %n;
k1=k2 %n;
a=a+new Character((char)k1).toString();
output=new TextField(null,a,100, TextField.ANY);
mForm.append(output); -
Leading Zeroes are lost when convert from string to int
What I'm trying to do is simple yet the solution has seemed difficult to find.
I have a system that requires 4 digit numbers as "requisitionNo". The system uses JSPs and accepts the 4 digit number that the user inputs (fyi - duplicate handling is already managed). The input number (rNumber) is of STRING type and in the action (using struts) is converted to an int:
int requisitionNo = Integer.parseInt(rNumber);At that very line the issue is that when the user inputs a number with leading zeros such as: "0001" the 3 leading zeros are chopped off in the INT conversion. The application validation kicks in and says: "A 4 digit number is required" which is by design. The work around has been that the user has been putting in number that start with 9's or something like that, but this isn't how the system was intended to be used.
How do I keep the leading zeroes from being lost instead of saving a number "1" to the database how do I keep it saving "0001" to the database? Would I just change everything to STRING on down to the database? or is there another number type that I can be using that will not chop off the leading zeroes? Please provide short code references or examples to be more helpful.Yeah, I have to agree here that leading zeroes make no sense. I figured that out when I started to look into this problem. The only requirement that exists is that the user wants it to be a 4 digit number due to some other requirement they have themselves.
So what I'm gathering from what I've read in the responses thus far is that I should change the validation a bit to look at the STRING for the 4 required digits (which are really 4 characters; maybe I should add CLIENT side numeric validation; currently its doing server side numeric/integer validation; or maybe change up the server side validation instead???) and if they are ALL GOOD then let the application save the int type as it wants to. IE: Let it save "0001" as just "1" and when I come back to DISPLAY this saved number to the user I should append the string of "000" in front of the 1 for display purposes only? Am I understanding everyone correctly? -
String to Int and Int to String
How can I convert a string to Int & and an Int to String ?
Say I've
String abc="234"
and I want the "int" value of "abc", how do I do it ?
Pl. help.String.valueOf(int) takes an int and returns a string.
Integer.parseInt(str) takes a string, returns an int.
For all the others long, double, hex etc. RTFM :) -
How to read a C structure with string and int with a java server using sock
I ve made a C agent returning some information and I want to get them with my java server. I ve settled communication with connected socket but I m only able to read strings.
I want to know how can I read strings and int with the same stream because they are sent at the same time:
C pgm sent structure :
char* chaine1;
char* chaine2;
int nb1;
int nb2;
I want to read this with my java stream I know readline methode to get the first two string but after two readline how should I do to get the two int values ?
Any idea would be a good help...
thanks.
Nicolas (France)Does the server sent the ints in little endian or big endian format?
The class java.io.DataInputStream (with the method readInt()) can be used to read ints as binary from the stream - see if you can use it. -
Missing charsets in STring to FontSet conversion
Hi,
After installed SUn OS 10, I am trying to install SMC and I got a warning error: Missing Charsets in String to FontSet conversion. Cannot convert string "-monotype_arrial-regular-r-normal --*-140-*-*-p-*-iso8859-2" to type FonsStruct
and so on, with different font types. etc....
How can I find out which fontset in the Unix box? How can I permanent set it back to UTF-8 w/o re install OS again?
Thanks
HSToo bad Lily didn't follow up on TommyReynolds' suggestion. I'm also having the same problem ( I think ).
TommyReynolds does say how to get the installed list, but doesn't say how to find out what's missing.
My settings:
[root@OEL5u4DB11gR2 X11]# rpm -qa 'xorg*font*' | /bin/sort
xorg-x11-fonts-100dpi-7.1-2.1.el5
xorg-x11-fonts-75dpi-7.1-2.1.el5
xorg-x11-fonts-base-7.1-2.1.el5
xorg-x11-fonts-ISO8859-1-100dpi-7.1-2.1.el5
xorg-x11-fonts-ISO8859-1-75dpi-7.1-2.1.el5
xorg-x11-fonts-misc-7.1-2.1.el5
xorg-x11-fonts-truetype-7.1-2.1.el5
xorg-x11-fonts-Type1-7.1-2.1.el5
xorg-x11-font-utils-7.1-2
[root@OEL5u4DB11gR2 X11]# uname -a
Linux OEL5u4DB11gR2 2.6.18-164.el5 #1 SMP Thu Sep 3 02:16:47 EDT 2009 i686 i686 i386 GNU/Linux
[root@OEL5u4DB11gR2 X11]# xclock &
[1] 5950
[root@OEL5u4DB11gR2 X11]# Warning: Missing charsets in String to FontSet conversion
I would greatly appreciate any input.
Kind regards,
UFP1701 -
Error Warning: Missing charsets in String to FontSet conversion
Hi
I was installed aqualogic BPM version 6.0 MP4 for weblogic on solaris 10 SPARC
when i ran albpmadmcenter i got an error
bash-3.00$./albpmadmcenter
Warning: Missing charsets in String to FontSet conversion
Warning: Missing charsets in String to FontSet conversion
any solution?
thanks.I just found this on the web at http://www-01.ibm.com/support/docview.wss?uid=swg21146408.
When I ran "export LC_ALL=C" and then ran xclock, I did not get the message. This might work for you.
Paul -
How to convert String to int in JSP?
Hi,
I set a session attribute in Servlet and want use it in JSP, How can I convert it to int or Integer?
the line in my code doesn't work:
int quantity=(int)session.getAttribute("vehiclequantity") ;
Thanks in advance.
Wolf
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<HTML>
<HEAD>
<TITLE>Using the for Statement</TITLE>
</HEAD>
<BODY>
<H1>Using the for Statement</H1>
<%=session.getAttribute("vehiclequantity") %>;
<%
int loopIndex;
int quantity=(int)session.getAttribute("vehiclequantity") ;
out.println(quantity);
for (loopIndex = 1; loopIndex <=2; loopIndex++) {
out.println("This is iteration number "
+ loopIndex + "<BR>");
%>
</BODY>
</HTML>Learning how to read errors and understand them will help you solve your problems quicker by yourself... So lets take a look at the error and classes involved...
The error says:
"Cannor Resolve Symbol: method valueOf(java.lang.Object) in the class java.lang.Integer" and gives you line line where the error occurs: Integer quantity = Integer.valueOf(session.getAttribute("vehiclequantity"));
Now, if we look at the API for the Integer we notice that are are only two valueOf methods: valueOf(java.lang.String s) and valueOf(java.lang.String s, int radix). Not valueOf(java.lang.Object) method.
Now we look at the getAttribute(java.lang.String name) method of HttpSession we see that the method returns a java.lang.Object. Now, you know you put a java.lang.String into that attribute, but the get method returns an Object. This is because you could have put any object in there, an Integer, a String, or some other class instance. But you know it is a String, so you can cast the returned value to a String, so that you will be calling the valueOf(java.lang.String s) method of Integer with the Object returned from the HtttpSession's getAttribute(java.lang.String name) method:
Integer quantity = Integer.valueOf((String)session.getAttribute("vehiclequantity")); -
Kinda urgent please help pass strings to doubles and strings to ints
Need to know how to pass strings to doubles and strings to ints
and to check if a string is null its just if (name == null;) which means black right?
like size as a string and then make the string size a doublecupofjava666 wrote:
Need to know how to pass strings to doubles and strings to ints
and to check if a string is null its just if (name == null;) which means black right?
like size as a string and then make the string size a doubleThink he means blank.
Check the Wrapper classes (Double, Integer) in the api.
parseInt() parseDouble() both take a string and return a primitive.
String s = null;
if(s == null) should do the trick!
Regards.
Edited by: Boeing-737 on May 29, 2008 11:08 AM -
Checking a value...String OR int
checking a value...String OR int
how to check a given value whether it is a String OR int
if(jTextField1.getText()...) is a String
System.out.println("String");
else //if it an int
System.out.println("int");
How can i check this...
pls,tell.getText of JTextField ALWAYS returns a String.
You can do this to see if it could be interpreted as an int:
String theText = jTextField1.getText();
try
int theInt = Integer.parseInt(theText);
System.out.println("int");
catch (NumberFormatException ex)
System.out.println("String");
}If it isn't in a form that can be parsed as an int, you will get an exception and the catch block will get executed. -
i have proubleam with string to date conversion, i out put date fromat is 2012-04-30T23:48:55.727-07:00 . so please help me the format conversion.
i wrote the method but it not workig
My method is
-(NSDate *)dateformstr:(NSString *)str
NSString *date = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSDateFormatter *dateFormate = [[NSDateFormatter alloc] init];
[dateFormate setDateFormat:@"yyyy-MM-dd'T'HH:mm:sssZZZZ"]
// NSDate *formatterDate = [dateFormate dateFromString:str];
return formatterDate;
but i did not the value and if i try othere formate i is working but my requiremet format is 2012-04-30T23:48:55.727-07:00.
can any help it out in this senario.Sorry Butterbean, but I'm interested in the answer to your question myself.
I've spent a few hours transfering my library from one computer to another and then find out that my ratings didn't transfer. Like you, I've spent many hours rating my 2000+ songs. I'm sure you have more, nevertheless, I want to find out how to get those ratings. They still show in my iTunes on my laptop but, when I go to the iTunes folder and display the details of at song, no rating is there. If you find out how to get them to display there in the iTunes folder, it seems that would be the key.
Hope you get your answer soon. -
Warning: Missing charsets in String to FontSet conversion
Hi ,
I just installed eclipse on a linux box abd while running eclipse i get the folowwing error -
Warning: Missing charsets in String to FontSet conversion
and the font looks distorted with blocks between alphabetsdid you fix this problem? I'm getting the same error :
[bmollett@hextall ~]$ Warning: Missing charsets in String to FontSet conversion
Warning: Cannot convert string "-*-*-medium-r-*-*-*-120-*-*-*-*-*-*" to type FontSet
Warning: Missing charsets in String to FontSet conversion
Warning: Unable to load any usable fontset
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
Warning: No font found.
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
Warning:
Name: FONTLIST_DEFAULT_TAG_STRING
Class: XmRendition
Conversion failed. Cannot load font.
JVMDG217: Dump Handler is Processing Signal 11 - Please Wait.
JVMDG303: JVM Requesting Java core file
JVMDG304: Java core file written to /home/bmollett/javacore.20050804.102118.11000.txt
JVMDG215: Dump Handler has Processed Exception Signal 11.
I've tried both motif and gtk2 versions ... -
How can i cast array of string to int ?
while(st.hasMoreTokens()){
test[count] = st.nextToken();
count++;
}Since token deal with string, i have to pass them into String of array, but how can i convert all of them to int to perform arithmetic operation?public static void main (String[] args) throws IOException{
DataInputStream dis = null;
String dbRecord = null;
int tokenCount = 0;
int numOfQuestion = 0;
int questionnAireNum = 0;
int postCode = 0;
int age = 0;
int gender = 0;
String [] response = new String[10];
File f = new File("polldata.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
// read the first record of the database
while ( (dbRecord = dis.readLine()) != null) {
StringTokenizer st = new StringTokenizer(dbRecord, ",");
tokenCount = st.countTokens();
numOfQuestion = tokenCount-4;
String rquestionNum = st.nextToken();
questionnAireNum = Integer.parseInt(rquestionNum);
String rpostCode = st.nextToken();
postCode = Integer.parseInt(rpostCode);
String rAge = st.nextToken();
age = Integer.parseInt(rAge);
String rGender = st.nextToken();
gender = Integer.parseInt(rGender);
for(int i=0; i<numOfQuestion;i++){
response[i] = st.nextToken();
}hi how come when i cast the string as int it prompt me error as shown below ? I wonder what causes this because this is normally how i cast string to int, somehow it won work this way with token.
Exception in thread "main" java.lang.NumberFormatException: For input string: " 3"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.valueOf(Integer.java:553)
at test.main(test.java:43) -
Javascript, string to int
Dear Sir:
I try to convert string to int in flash javascript:
temp="120";
temp2="140";
temp3=int ( temp );
but I got a message "int is not a function", can somebody tell me how to do this?
also, what if I want convert number back to string?
thanks a lot!use:
temp="120";
temp2="140";
temp3=Number ( temp );
// to convert to a string, use:
var temp4:String=String(temp3);
// or
var temp5:String=temp3.toString();
Maybe you are looking for
-
Change the colour of the prompt and the value
Hi, I have an Item MessageStyledText with some value on an OAF Page. By default the colour of the Prompt and the value of this Item are black. But i need to change the Colour to Blue. Fast response highly appreciated. Thanks, Palepu.
-
No thumbnails in PP Media Browser
Hello, I am working on MacOS X with Adobe CS4 Production Premium. Our footage is either Sony XDCAM EX or AVCHD. The XDCAM files are copied via Sony's Clip Browser, i.e. with the complete BPAV structure. The AVCHD files are transfered via iMovie, resu
-
Multilevel menu -buletted menu item list
Hi, I have created a multilevel menu in my portal. The dropdown menu items appears correctly but in addition to the drop down menu I also get a bulleted list of menu items under the root menu. Can anyone tell me how to disable this bulleted list. The
-
Is complex type request supported by web service data set?
I just installed OBIEE 11g and tried to create data models with web services. It works fine for web services that takes simple data types and returns complex data types. But if the request to the web service is a complex type, it doesn't work. On the
-
"this iPod cannot be used because the Apple Mobile Device service is not started" This message is shown when I plug in my iPod to iTunes. How do I fix this?