Converting Character Representation of Integer to Integer in Java

  • Java
  • Thread starter bomba923
  • Start date
  • Tags
    Java
In summary, the conversation discusses different ways to convert a character representation of an integer into an actual integer. The first method involves using the Integer.parseInt() and String.valueOf() functions, while the second method suggests creating a new function to simplify the process. Despite the suggestion, the original method is deemed efficient and neat.
  • #1
bomba923
763
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
  • #2
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;
}
 

1. What is Java?

Java is a high-level, object-oriented programming language used for developing a variety of applications, from web and mobile applications to enterprise systems and video games.

2. Why is Java popular?

Java is popular because it is platform-independent, meaning it can run on any operating system, and it is also highly versatile and can be used for a wide range of applications.

3. What is the difference between Java and JavaScript?

Despite their similar names, Java and JavaScript are two completely different programming languages. Java is an object-oriented programming language used for creating applications, while JavaScript is a scripting language used for creating interactive web pages.

4. What are the main features of Java?

Some of the main features of Java include its platform-independence, object-oriented nature, strong memory management, and robustness. Java also has a large standard library and a vast community of developers.

5. Is Java difficult to learn?

Java can be challenging for beginners, but with dedication and practice, it can be learned by anyone. Its syntax is similar to other programming languages, and there are many resources available, making it easier to learn.

Similar threads

  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
28
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top