answersLogoWhite

0

Yes as long as the value of the string is an integer. String str = "12345"; int i = Integer.parseInt(str); Note: This code would work very well for strings that have numeric values inside. If your string can contain any kind of data, remember to surround this code with a try catch block to ensure that you don't get an exception that crashes your system. Some of us usually miss the try catch block expecting ideal input. But once in a while we get some data that is different to what we are expecting in our code which crashes our system. Instead of spending hours trying to figure out what crashed our app, its easier to have a catch block do the work :)

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

In java how do you use a for loop to print out a word as many times of its length?

Its simple: String a="Suraj Acharya"//consider this to be your string for(int i=0;i<a.length();i++) System.out.println("Its Simple");


How do you use indexOf in java?

indexOf is a method of the String class. Since the indexOf method is overloaded, I will be using the indexOf(String str) version in this example. According to the API Documentation, this method "Returns the index within this string of the first occurrence of the specified substring." So, if you wanted to find the position of the letter 'v' in the String 'Java' and print it out, you would do this: String str = "Java"; int i = str.indexOf("v"); System.out.println(i); If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1 (negative one).


Which is the best way to convert numbers in string under java?

All of the Java number classes have a parse[type] method, like parseInt() in Integer or parseDouble() in Double that convert Strings to primitive numbers. String s = getInput(); int var = Integer.parseInt(s);


How can you convert byte to string in java?

To convert byte to String in java use the String(bytes, UTF-8); //example for one encoding type. You must know the special encoding that contains a variety of characters.


What is member variable in java?

We use the term member variable to refer to variables that are defined inside a method. Ex: public String getName(){ String x = "ttt"; ..... } In the method getName() x is a member variable

Related Questions

How can someone convert string to int in Java?

To convert string to int in Java, the easiest way is to simply use the method Integer.parseInt(). For more information how to do this, refer to the integer class documents.


In java how do you use a for loop to print out a word as many times of its length?

Its simple: String a="Suraj Acharya"//consider this to be your string for(int i=0;i<a.length();i++) System.out.println("Its Simple");


How do you use indexOf in java?

indexOf is a method of the String class. Since the indexOf method is overloaded, I will be using the indexOf(String str) version in this example. According to the API Documentation, this method "Returns the index within this string of the first occurrence of the specified substring." So, if you wanted to find the position of the letter 'v' in the String 'Java' and print it out, you would do this: String str = "Java"; int i = str.indexOf("v"); System.out.println(i); If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1 (negative one).


Which is the best way to convert numbers in string under java?

All of the Java number classes have a parse[type] method, like parseInt() in Integer or parseDouble() in Double that convert Strings to primitive numbers. String s = getInput(); int var = Integer.parseInt(s);


How can you convert byte to string in java?

To convert byte to String in java use the String(bytes, UTF-8); //example for one encoding type. You must know the special encoding that contains a variety of characters.


What is member variable in java?

We use the term member variable to refer to variables that are defined inside a method. Ex: public String getName(){ String x = "ttt"; ..... } In the method getName() x is a member variable


Why we use IntegerParseInt in java?

Parsing is very important since the input from the user is not in the form of ints but in a String, therefore, you have to parse the String containing the number into a primitive data type. i.e. String num = "49"; int realNum = Integer.parseInt(num); // puts 49 into realNum;


How do you extract digits from an integer in java?

There are different ways to do it. One is to convert it to a String, then use the string manipulations methods to extract individual digits as strings. You can then convert them back to numbers. Another is to do some calculations. For example, to get the last digit: int i = 12345; int lastdigit = i % 10; //To get additional digits, divide by 10 and repeat: i /= 10; int lastdigit = i % 10; In this case you can create a loop for this (repeating while i > 0), and copy the digits to an array.


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


Concat a string without using any inbuilt function in java?

Use "+". Example: String string = "does this answer " + "your question?";


What is the code required to convert an integer variable to a string variable in Java?

There are several different methods to convert an integer variable to a string variable in Java. For example, one can use the following code to convert an integer variable to a string variable: Integer.toString(number)


How many bits does a Java char contain?

16 bits. Java char values (and Java String values) use Unicode.