Java Converting Character Representation of Integer to Integer in Java

  • Thread starter Thread starter bomba923
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
To convert a character representation of an integer into an actual integer in Java, the common approach involves using Integer.parseInt along with String.valueOf. The initial example demonstrates this by extracting a character from a string and converting it to an integer before adding another integer. While the method works without compilation errors, there is a desire for a more concise solution. A suggested alternative is to use Integer.parseInt with Character.toString, which streamlines the process slightly. Additionally, defining a custom function can encapsulate this conversion, allowing for cleaner code when performing the operation multiple times. Overall, while there are ways to simplify the conversion, the standard method remains effective and clear.
bomba923
Messages
759
Reaction score
0
:redface: Is there a way to convert a character representation of an integer into an integer (with integer representation :rolleyes:)?
*for example (I'll add 8 to a certain digit in an integer),

String nmbr; //where I enter a number
int pst; //an integer representing the position of that digit in the number

System.out.println( nmbr.charAt(pst) + 8 );

---------
Obviously, I cannot add a character and an integer. Thus, I change to:
---------

System.out.println( Integer.parseInt(String.valueOf(nmbr.charAt(pst))) + 8 );

-----------------------
and it works, no compiling errors.

*However, is there a shorter way of converting a character representation of an integer to...well, an integer? :frown: Shorter than typing out:

" Integer.parseInt(String.valueOf(nmbr.charAt(pst))) " ?
 
Technology news on Phys.org
I would rather not prefer a shorter option. Integer.parseInt(String.valueOf(nmbr.charAt(pst))) is a neat code. The best you can do is Integer.parseInt("" + (nmbr.charAt(pst)), and one function call can be eliminated.

However, if you want, you can always define a new function:
Java:
int convert_add(char x, int y){
    int sum = Integer.parseInt(Character.toString(x)) + y;
    return sum;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top