954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String to Int conversion

i am doing the coding of my minor project and for that i need to convert an input text(String) to an int type as i am inputting the data through swing component, panel as text(string). But internally i need to process that as int. so please tell me how can i convert it. For example, if my input String is "786", i should get its int value as 786.
please provide some code if possible.

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 

Something like this...

String input = "123";
int parsedInput = 0;
try {
   parsedInput = Integer.parseInt(input);
} catch(NumberFormatException nfe) {
   // Handle incorrect input
}
SasseMan
Junior Poster
176 posts since Jan 2010
Reputation Points: 70
Solved Threads: 19
 

but what if my input string will be itself a string like this:

String input = "adil";

and i want to convert it into int like its ASCII value.

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 

but what if my input string will be itself a string like this:

String input = "adil";

and i want to convert it into int like its ASCII value.


well you'd first make an int[] thats equal to the size of the string ie string.length() then you'd have a for statement beginning at
0 and looping for iString input = ...;
int[] values=...;
for(int i=0;i<input.length();i++) {
values[i]=(int)input.charAt(i);
}

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

please provide full code as i dont know much about such problem

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 
please provide full code as i dont know much about such problem


the code is basically done?:

int[] values=new int[input.length()];


the other im sure you can figure out ;)

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

i think the result will be array of type int, but in my later pices of code, i need to perform the division of the result obtained with some int value. can i do so with int array or tell me how it can be done? provide code if posible

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 
i think the result will be array of type int, but in my later pices of code, i need to perform the division of the result obtained with some int value. can i do so with int array or tell me how it can be done? provide code if posible


well of course it depends would you need to preform division on each individual letters' ASCII or the concatenated result, either way you'd just have to parse it to an integer if you need the entire result, or use it as is to preform division on each result in the array

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

please help for concatenated result?

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 
please help for concatenated result?
String input = "abc";
        String tmp="";
                for(int i=0;i<input.length();i++)  tmp+=""+(int)input.charAt(i);
DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

Where will be the int value, the declaration of tmp is string??

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 
Where will be the int value, the declaration of tmp is string??


yes now all you do is convert the string 'tmp' which holds the concatenated ascii values to a int like so:

int val=Integer.parseInt(tmp);
DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

thanks

adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You