Getting an integer from the beginning of a string?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I'm trying to obtain an integer from the beginning of a string. The
integer is of arbitrary length, followed by a delimiter, followed by
the rest of a string (which I need to preserve to do further parsing
on. ie, if ; is the delimiter, then the string might look like:

3;sometext or 342;sometext

To make things worse, the leading integer might not be an integer - it
might be a letter that I need to recognize as a delimiter.

So what I want to to do is identify that the leading part of the
string is in fact an integer, at which point I can split the string on
the delimiter and continue parsing. I've tried to use
Integer.getInteger:

Integer TestInteger = Integer.getInteger((InputString.substring(0,
1)));

But this returns null for some reason, even when run on an integer (as
a string).

How can I correctly identify that there is an integer here?

Thanks!
 
D

Daniele Futtorovic

I'm trying to obtain an integer from the beginning of a string. The
integer is of arbitrary length, followed by a delimiter, followed by
the rest of a string (which I need to preserve to do further parsing
on. ie, if ; is the delimiter, then the string might look like:

3;sometext or 342;sometext

To make things worse, the leading integer might not be an integer - it
might be a letter that I need to recognize as a delimiter.

So what I want to to do is identify that the leading part of the
string is in fact an integer, at which point I can split the string on
the delimiter and continue parsing.

Can't you simply check the characters? For instance using
Character.isDigit(char)?

Or maybe first split the String using the delimiter, and then check
whether it's an integer using Integer.parseInt(String).
I've tried to use
Integer.getInteger:

Integer TestInteger = Integer.getInteger((InputString.substring(0,
1)));

But this returns null for some reason, even when run on an integer (as
a string).

Check the Javadoc:

public static Integer getInteger(String nm)
Determines the integer value of *the system property with the
specified name*.


API docs are to be found here:
<http://java.sun.com/javase/6/docs/api/>

Tutorial for working with Numbers and Strings:
<http://java.sun.com/docs/books/tutorial/java/data/index.html>


DF.
 
N

nooneinparticular314159

On 28.01.2008 05:16, (e-mail address removed) allegedly wrote:

Can't you simply check the characters? For instance using
Character.isDigit(char)?

Thanks! That was exactly right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top