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.

Something like this...

String input = "123";
int parsedInput = 0;
try {
   parsedInput = Integer.parseInt(input);
} catch(NumberFormatException nfe) {
   // Handle incorrect input
}
Comments
no idea why this should be negative, since it is an answer to what the OP originally asked

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.

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 i<string.length(). then you would use the charAt() method and put each int value of the char into
the int[] you created and from there you can output it, and change the code to instead concactenate it or whatever:

String input = ...;
        int[] values=...;
        for(int i=0;i<input.length();i++) {
            values[i]=(int)input.charAt(i);
        }

Edited 4 Years Ago by DavidKroukamp: n/a

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

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

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

please help for concatenated result?

String input = "abc";
        String tmp="";
                for(int i=0;i<input.length();i++)  tmp+=""+(int)input.charAt(i);

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

Edited 4 Years Ago by DavidKroukamp: n/a

This question has already been answered. Start a new discussion instead.