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
-3
votes
2 answers

NumberFormatException: Invalid int:""

In my app, I am doing language localization.it works perfectly with buttons and texviews but cant change the string on the recyclerview it says NumberFormatException: Invalid int:"" private void updateViews(String languageCode) { …
-3
votes
1 answer

Why does parseInt('1 year old') return 1 but parseInt('I am 1 year old') returns NaN?

The code ran in developer console is shown below. Why does parseInt return the number present in [0]th index of a string but not return the number present in any other index in the string? parseInt('i am 1 year old') // output NaN parseInt('1 year…
Vinod kumar G
  • 639
  • 6
  • 17
-3
votes
2 answers

Javascript parseInt - Code running 4 times

For this function, I am trying to calculate the outcome of the $r variables and display them in a form input text field, "otherHtmlIdName". The if statement checks to see if they all have values, then gets the calculated variable's value and changes…
H311B0Y
  • 3
  • 4
-3
votes
1 answer

isNaN not working JavaScript

I’m having trouble with the isNaN function is JavaScript. I have a variable, trc, which I know is not a number, but I want my code to be able to tell. Unfortunately, isNaN isn’t detecting that it’s not a number, yet when I use an alert to show the…
ColorCodin
  • 101
  • 2
  • 11
-3
votes
1 answer

conversiton to number doesn't work

Can anyone explain to me why this code doesn't works correctly: var num = '10'; Number(num); console.log(typeof(num));//string parseInt(num); console.log(typeof(num));//string parseFloat(num,…
Muribury
  • 3
  • 4
-3
votes
2 answers

What does this code mean exactly?

I am just not sure exactly what this section of code means. try { startGame(Integer.parseInt(clickedButton.getLabel())); } catch (Exception ex) { Logger.getLogger(JavaGame.class.getName()).log(Level.SEVERE, null, ex);
-3
votes
2 answers

NumberFormatException trying to parse " 66" to an integer

So i have this slice of code that reads text from a csv file where nums[] goes throughout the file and stores the number of a said student, and grades[][] goes through and stores each of their grades: Scanner sc = new Scanner(new…
desidia
  • 3
  • 2
-3
votes
5 answers

Checking for a specific characters in Strings

I have the following code that reads a file that has 3 integers in the formats of a single number, or x/y where x is an int as is y. I'm trying to read the file, then split at each white space in order to isolate each part of the string to end up…
Sim
  • 570
  • 1
  • 10
  • 22
-3
votes
2 answers

javascript parseInt

Possible Duplicate: How to parseInt a string with leading 0 If I parseInt("01") in javascript its not the same as parseInt("1")??? start = getStartEugene("MN01"); start2 = getStartEugene("MN1"); getStartEugene: function(spot) //ex: GT01 GT1 { …
ealeon
  • 12,074
  • 24
  • 92
  • 173
-4
votes
1 answer

why parseInt here causes NumberFormatException inside the for loop at index i=4

I don't know the reason, why i=4 cause Numberformatexception, I can use try and catch to handle the Numberformatexception, but i actually don't know the reason for this mistake, and how can i fix it without using try,catch block ,can someone help,…
-4
votes
2 answers

Integer.parseInt() statement shows array index out of bound error while taking command line input in java

I was trying to take three command line arguments as input and do some basic maths operations, but the compiler is showing arrayindex out of bound error in lines having Integer.parseInt(). public class testarray { public static void…
-4
votes
2 answers

Jquery ParseInt does not work

$('document').ready(function(){ var totp=11; var ids=""; for(var c=1;c < totp;c++){ var rateid='input#'+'rate'+c; var qtyid='input#'+'qty'+c; var discid='input#'+'disc'+c; var surcid='input#'+'surc'+c; var totid='input#'+'tot'+c; …
user3526204
  • 509
  • 5
  • 22
-4
votes
1 answer

Why does "console.log(parseInt(0o22,8))" display "1"

Why does console.log(parseInt(0o22,8)) output 1?
Minzer
  • 9
-4
votes
2 answers

NaN being returned on parseInt()

http://jsfiddle.net/leongaban/fhhmLfab/ In the example above I have 3 buttons, orange, green and red. On hover each reveals a [ - ] and a [ + ] button with a class of btn_action. On click I'm trying to grab the numerical value from the target color…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
-4
votes
2 answers

JavaScript string to integer

I'm making a conversion app and I keep getting return Not a Number (NaN) function conversion() { var bill = document.getElementById('bill'); var mates = document.getElementById('mates'); …
1 2 3
51
52