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

Can't Use Parseint

I m just a beginner .And whenever I run this program it just ends... Please help... the error is below ,,whenever i delete this ,the program works @Override protected void onCreate(Bundle savedInstanceState) { …
-2
votes
2 answers

How do i get data from a form , convert it to an int and pass it to a java class?

.jsp <% MainProgram.MainAverageProgram myProgram = new MainProgram.MainAverageProgram(); %> <% out.println("" + myProgram.getForm()); %> // returns simple form with parameter user <%…
Aaron
  • 75
  • 8
-2
votes
2 answers

Integer.parseInt("-1") result an error

I'm writing new accounting program for my customer on Android,In some case I need to convert text of EditText to integer,but when I want to parse "-1" the java.lang.NumberFormatException occured! for understanding my problem try this: int i =…
Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35
-2
votes
1 answer

parseInt error in my code

I get error messages in my code. Code and errors is below. public class Main { public static void main(String[] args){ String sayi[] = {"37107287533902102798797998220837590246510135740250", …
oPsimat
  • 1
  • 1
-2
votes
2 answers

Error in .getText() in a parseInt in Java

I know the title might not be that helpful. I do apologize. My problem is that I am trying to create a simple MPG application to help me get used to GUI. I have a gallons and a miles JTextField. I also have an ueditable JTextField to display the…
engz
  • 45
  • 1
  • 1
  • 10
-2
votes
3 answers

Casting a String Array to ArrayList

I am trying to import a web file containing a numbers that correspond to vertices for a graph. The first line states how many vertices there are. So i defined an int verticesAmount to correspond to how many vertices there are. After that I…
seiko149
  • 19
  • 6
-2
votes
1 answer

I can't seem to calculate with modulus

Its been a few months since I graduated from a tech school (HS level) for programming, and my JavaScript is a bit rusty. I'm just trying to calculate Y%X=Z with textbox inputs. However Z always results in NaN, so I'm assuming my problem is the…
-2
votes
2 answers

Android app FC when converting to Integer

I have an android app which has a few edittext boxes. I need to convert one of the edittext box value to use for calculation. I have the following code: final EditText taken = (EditText)findViewById(R.id.editText1); String…
Si8
  • 9,141
  • 22
  • 109
  • 221
-2
votes
2 answers

NumberFormatException when attempting to parse string as an integer

Exception in thread "main" java.lang.NumberFormatException: For input string: " 400" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:470) at…
한재진
  • 1
  • 1
  • 5
-3
votes
1 answer

How to convert unicode to int in java?

My java program won't convert unicode to int, but will convert ascii to int. The first line works and converts ascii 53 to int 5. However, the second line with the unicode value stops the program. first =…
Jon
  • 1
  • 3
-3
votes
1 answer

i can't understand parseInt method in this code

code is like that :) function test3(num) { if (num <= 9) { return num; } let rest = 1; while (num) { rest = rest * (num % 10); num = parseInt(num / 10); } if (rest <= 9) { return rest; } return…
Loolii
  • 397
  • 3
  • 9
-3
votes
1 answer

Java parse hexadecimal as a integer

When i paste parseInt("3cf1bb13f1c96", 16) to javascript console, it successfuly converts to integer value which is 1072142774901910 but when i use Integer.parseInt("3cf1bb13f1c96", 16) in java it prints something Exception in thread "main"…
Garbarius
  • 5
  • 2
-3
votes
2 answers

WITHOUT using parseInt or toString, write a function that will take a binary string and convert it to a number

Binary describes a number expressed in only 1s and 0s. Learning binary simply means learning a new way that count and calculate numbers. You really don't need to know binary for most cases while coding; however, some coding challenges that you could…
-3
votes
1 answer

parseInt() & radix basics

Can someone please explain what radix is (on the most fundamental level), in reference to parseInt()? I'm also not understanding how the string argument changes with different bases/radix.
-3
votes
2 answers

How do I show only numbers from a Javascript array that has both numbers and letters use parseInt?

I'm trying to use the parseInt to show only numbers from the following array: 1,2,a,b Here is my javascript code so far: var filter_list = ["1,2,a,b"]; function myFunction() { parseInt(filter_list); return…
Brent Nicolet
  • 345
  • 1
  • 5
  • 9