Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sample.java:7: error: cannot find symbol
i=Integer.parseInt(s);
^
symbol: method parseInt(String)
location: class Integer

error has occure while converting string to integer

java version "1.8.0_191"
javac 1.8.0_161

What I have tried:

class sample
{
	public static void main(String args[])
	{
		String s=new String("100");
		int i;
		i=Integer.parseInt(s);
		System.out.print(i);
	}	
}
Posted
Updated 20-Nov-18 6:26am
Comments
Richard MacCutchan 20-Nov-18 12:19pm    
I just tried that code and it works fine.

First off, the code works just fine and the output is 100 in my case, just tried in VS Code. So, show the code that is causing the problem.

Now, for the question, these two errors are different,
sample.java:7: error: cannot find symbol
and,
error has occure while converting string to integer

They both have a different meaning, thus sharing that they occur at 2 different instances. The first one, would mostly mean that you might be missing an import. Whereas, the second one meaning that you has passed in an in correct value of string, that cannot be mapped to a valid integer. A few things you can do,
Java
int value;
try {
   value = Integer.parseInt(s);
} catch (Exception e) {
   // Problem, 
}
This will ensure that incase there is a runtime exception during conversion, your app will not crash, instead will be given an opportunity to render a friendly message on the screen. If you try to check the documentation for the parseInt() function, you will see that it throws NumberFormatException, that is what you need to look for.

Integer (Java Platform SE 7 )[^]
 
Share this answer
 
import java.lang.Integer;
class sample
{
	public static void main(String args[])
	{
		String s=new String("100");
		int i;
		i=Integer.parseInt(s);
		System.out.print(i);
	}	
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 21-Nov-18 3:57am    
That import is not required, it works without that too as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900