Extracting a number out of a string in Java

  • Context: Java 
  • Thread starter Thread starter nkk2008
  • Start date Start date
  • Tags Tags
    Java String
Click For Summary

Discussion Overview

The discussion revolves around extracting numerical values from a string formatted as "int,int,int,int" in Java. Participants explore methods for converting string representations of numbers into integer or double types, addressing issues related to data types and parsing.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant suggests using String.split(",") to create an array from the string and then using Integer.parseInt to convert each element to an integer.
  • Another participant expresses gratitude for the introduction of String.split, indicating it simplified another part of their task.
  • A participant shares test code and encounters a NumberFormatException when attempting to parse a string containing a decimal number (1.5) as an integer.
  • Some participants clarify that 1.5 is not a signed decimal integer, prompting questions about how to convert it to an integer and what constitutes a signed integer.
  • Common methods for converting real numbers to integers, such as truncation and rounding, are mentioned.
  • A participant expresses a need to maintain the decimal nature of numbers for a physics model, leading to a realization that doubles may be more appropriate.
  • Later, the participant acknowledges that Double.parseDouble is a suitable method for extracting doubles from a string.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach initially, as there is confusion regarding data types and parsing methods. However, there is agreement on the need to use doubles for maintaining decimal precision.

Contextual Notes

The discussion includes assumptions about data types and parsing methods that may not be fully resolved. There is also a lack of clarity around the concept of signed integers and how they relate to the participant's needs.

Who May Find This Useful

Readers interested in Java programming, particularly those dealing with string manipulation and numerical data types, may find this discussion beneficial.

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 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K