Java Extracting a number out of a string in Java

  • Thread starter Thread starter nkk2008
  • Start date Start date
  • Tags Tags
    Java String
AI Thread Summary
To extract integers from a string formatted as "int,int,int,int" in Java, the initial approach involves using String.split(",") to create an array of string values. However, an issue arises when trying to parse decimal numbers, as Integer.parseInt only accepts whole numbers. The user encountered a NumberFormatException when attempting to parse a string containing a decimal (e.g., "1.5"). This led to confusion about the nature of signed integers and the need for precision in their application, specifically for modeling speeds in a physics context. After further research, the user realized that using Double.parseDouble would be the appropriate method for extracting double values from the string, allowing for the retention of decimal precision.
nkk2008
Messages
32
Reaction score
0
I have a string of the form "int,int,int,int" and I want to extract each of those ints into a variable that is an int. I have no idea where to even start (I am learning Java as I make a program for an internship, and my searching has yielded nothing).

Thanks,
Nkk
 
Technology news on Phys.org
You can use String.split(",") to break the string into an array delimited by comma. Then loop through the array and use Integer.parseInt before pushing it into another array.
 
DavidSnider said:
You can use String.split(",") to break the string into an array delimited by comma. Then loop through the array and use Integer.parseInt before pushing it into another array.

Thank you. You actually made another part much easier by introducing Sting.split to me.


Thanks,
Nkk
 
Ok, so here is some test code I made to quickly test how this would work:

String string = "1.5,2.3,0.4,2.45,245.4542,245.875,0.785,0.56765,56765.567";
String[] stringarray = string.split(",");

int test;

for(int i = 0; i<stringarray.length; i++){
System.out.println(stringarray);
test = Integer.parseInt(stringarray);
System.out.println(test);
}

And this is the error I get:
1.5
Exception in thread "main" java.lang.NumberFormatException: For input string: "1.5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:458)
at java.lang.Integer.parseInt(Integer.java:499)
at test.main(test.java:79)

Line 79 in my program is "test = Integer.parseInt(stringarray);"

The API entry for parseInt says " Parses the string argument as a signed decimal integer." Thus, I assumed the decimal nature of my ints would be kept intact. Was that a wrong assumption?

Thanks
Nkk
 
1.5 isn't a signed decimal integer.
 
Hurkyl said:
1.5 isn't a signed decimal integer.

So...um...how do I convert it to a signed int? What is a signed int? I looked online, and am sort of confused.

Thanks,
nkk
 
Common operations for converting real numbers into integers are "truncate" and "round".
 
Ok, (and this is where my total lack of any real knowledge comes in), how do I keep its decimal nature? In reality, these numbers are speeds that will be fed into a physics model for total energy used, so they need to be pretty exact.

Also, can someone explain exactly what makes an int signed?

Thanks for all the help,
Nkk

EDIT: Ok, further research has sort of explained this to me, and I have come to the conclusion I want to use doubles. So..is there a way to extract a double from a string?

EDIT2: So it seems I am totally idiotic, as Double.parseDouble is an obvious thing to use. Thanks to all who helped me.
 
Last edited:

Similar threads

Replies
2
Views
2K
Replies
3
Views
1K
Replies
2
Views
13K
Replies
6
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
Replies
11
Views
2K
Replies
1
Views
3K
Back
Top