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

How do I print both Strings and Ints from a single text file?

I have a file pretty much as follows, but for a couple more names (each word and number is on a separate line) : James 123 343 355 Kyle 136 689 680 I’ve been trying to read all these in as strings, and use parseInt to convert them to ints, as I need…
-2
votes
1 answer

Integer.parseInt converts everything to zero

Hello everyone i am new to java and i have a problem. I accept a number with the String type, then I write it to the txt variable, then I overwrite it and add "\n", and then I try to convert it to the int type, but no matter what number is, it…
Viktor
  • 3
  • 2
-2
votes
1 answer

Why is the variable String type, even though I converted it to int?

The variable int g is supposed to be a int, but on output it is being taken as a String, can't figure out what's wrong. class scantest { public static int String_to_Int(String a) { int n=Integer.parseInt(a); …
-2
votes
2 answers

Is their any way I can **not** round off the number while using parseInt?

let display = '5+10.10'; let numbers = display.match(/(\d+\.?\d*|\.\d+)/g).map(a => parseInt(a)); console.log(numbers)//returns [5, 10] when I don't use parseInt: let display = '5+10.10'; let numbers =…
-2
votes
2 answers

Beginner needs help (parseInt). JS/HTML

I started to learn this coding thing over a week ago and I don't have much experience in it ( as you probably see). I have no idea what I'm doing, therefore my code doesn't work, For some reason it prints only first 2 answers. If you could just let…
CKh911
  • 1
  • 1
-2
votes
1 answer

Parsint Query always showing as NaN

With the code below I get 'Your score is NaN' and I cannot work out why. The final code will add up all the questions in the quiz. In the browser I can see that the value of the radio is being correctly picked up and i use parseint to convert it to…
DRH09
  • 5
  • 3
-2
votes
2 answers

parseInt is trimming trailing zeroes after comma javascript

I am stuck in a solution in which I am trying to convert the string to integer, but it truncates the zeroes after comma.Any help on this is much appreciated. For example, parseInt("3,50,000") // 3 ==> what i actually need is 3,50,00 of integer…
Nancy
  • 911
  • 7
  • 26
  • 54
-2
votes
1 answer

Need Guide to Use the parseInt Function

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function/ I need the guide to Basic JavaScript - Use the parseInt Function (at freecodecamp)
-2
votes
1 answer

Can't pass converted integer to a database

I have this script that is supposed to add an integer to an SQLite database. //SIMPLIFIED SCRIPT FOR DEMONSTRATING PURPOSES private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { …
Smirlianos
  • 5
  • 1
  • 6
-2
votes
1 answer

How do I use parseInt to add two numbers?

I'm supposed to use parseInt to add two numbers the user enters, then use my response object to send back the result and display output to the user. For instance, if the user enters 2 and 3, the server should add these numbers together and send back…
-2
votes
3 answers

Exception Handling parse int and empty string

name = full_name_input.getText(); Fname = father_name_input.getText(); cnic = father_cnic_input.getText(); DOB = Integer.parseInt(DOB_input.getText()); Class_V = Integer.parseInt(class_input.getText()); prsnt_add =…
Abdul Wahab
  • 99
  • 1
  • 2
  • 11
-2
votes
1 answer

Unresolved compilation problem: The method parseInt(String) is undefined for the type Integer

Why it does not like parseInt? How to fix it? import java.lang.*; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import…
Rabbit
  • 39
  • 3
  • 6
-2
votes
1 answer

Strange behaviour when parsing text to int javascript

Alright, This one is a bit hard to replicate probably, but I cannot find the source of the problem.. When I try to do a parseInt on the test[0], it returns NaN: var test = ["‎02", "09", "2015 17:34"]; var result = parseInt(test[0], 10); // Return…
DutchKevv
  • 1,659
  • 2
  • 22
  • 39
-2
votes
2 answers

Javascript yearly compound interest calculation

When this code runs, it outputs "Annual Account Balance for Year (i) = 100" for every year. Clearly the answer shouldn't be 100 every time, but I can't see what I am doing wrong in the equation. var invest; var rate; var amount; rate =…
snazz
  • 21
  • 3
-2
votes
1 answer

PHP parse int wrong in XAMPP windows

demo $code = '40001042901'; echo (int)$code; //intval($code) //same I test on linux (Ubuntu) that result is 40001042901 but on windows result is 2147483647, what wrong with that? XAMPP 1.8.3, PHP 5.5.15, Apache 2.4.10 (Win32)
QHu91_IT
  • 189
  • 2
  • 12