Questions tagged [parseint]

parseInt is a function that returns the integer representation of a given string.

parseInt is a function that returns the integer representation of a given string. It requires one parameter, but usually it can accept two parameters:

parseInt(toParse)
parseInt(toParse, radix)

Where toParse is a string, and radix is an integer. When the one-parameter function is called, a radix of 10 is used for the conversion.

In Javascript the syntax is parseInt(toParse, radix) Examples of use with :

document.write(parseInt("10") + "<br>");
document.write(parseInt("82", 16) + "<br>");
document.write(parseInt("11.25") + "<br>");
document.write(parseInt("23 11 55 32") + "<br>");
document.write(parseInt(" 7 ") + "<br>");
document.write(parseInt("45 programmers") + "<br>");
document.write(parseInt("I am 23") + "<br>");

Output:

10
130
11
23
7
45
NaN

In Java the syntax is Integer.parseInt(toParse, radix). Unlike javascript, with java you have to be sure that you try to parse an integer. Parsing 11.25, 23 11 55 32 or 45 programmers will fire a NumberFormatException.

Examples:

System.out.println(Integer.parseInt("10"));
System.out.println(Integer.parseInt("82", 16));
System.out.println(Integer.parseInt("11.25"));
System.out.println(Integer.parseInt("23 11 55 32"));
System.out.println(Integer.parseInt(" 7 "));
System.out.println(Integer.parseInt("45 programmers"));
System.out.println(Integer.parseInt("I am 23"));

Output:

10
130
Exception in thread "main" java.lang.NumberFormatException: For input string: "11.25"
774 questions
-4
votes
3 answers

How do I parse numbers in a string?

I have a string 'line' in java containing numbers , for example , "34 55 64 " I want to store these 3 numbers in int x , y ,z when I do , x = Integer.parseInt(line[0]) y = Integer.parseInt(line[1]) I get an error saying a "array required but…
user2623946
  • 55
  • 1
  • 13
-4
votes
2 answers

how can I implement my parseInt(String str) method?

Here is my task. I have no idea how to implement this method. Implement a method of Integer parseInt (String str) throws NumberFormatExceptionwhich will take the input string, which must contain onlynumbers and do not start from zero, and return a…
Volo Shima
  • 3
  • 1
  • 4
-4
votes
1 answer

Why java not taking numbers from GUI?

I created a loan calculator. but its not taking value for calculation. could you please help me to find what did go wrong please. import java.awt.*; import javax.swing.*; public void initUIPanel() { jf = new JFrame(); …
Shasi
  • 274
  • 6
  • 21
-5
votes
3 answers

Android App crashes at line 54(Integer.parseInt) and not entirely sure why

I've been debugging my app with my phone and all the logcat errors I get refer to line 54 in my activity where I parse a String into an Int. The basic idea of the app is a penny converter in which the user enters the number of pennies they wants to…
-5
votes
2 answers

ParseInt Error - Java

Why am I getting this error? I've tried changing many aspects of the code, but could not figure out the error. Thank you! import java.util.*; public class Problem1 { public static void main(String[] args) { int ogarank = 177; …
Icy
  • 17
  • 4
-5
votes
1 answer

Get data type from String when the String have symbol

I want to have a int from String when the String have a symbol in the EditText. How i get it?. for example: when input in EditText like Rp.1.000.000.000, and i will get an integer with value 1000000000. please, answer my question in java language.…
-6
votes
3 answers

Number format exception in parse int

public static void main(String[] args)throws IOException { String s ="12312a"; int x = Integer.parseInt(s); System.out.println (x+2); } and all what I've got is : Exception in thread "main" java.lang.NumberFormatException: For input string:…
-9
votes
1 answer

What does parseInt do in the following function?

The following function is designed to take numbers out of a string and then add those numbers. For example, you can enter a string like "What does the iphone6 cost in 2015?" and it should return 2021. I was able to do 90% of the function, but…
kodave
  • 1
  • 3
1 2 3
51
52