Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parsing a string to get integers

Reply
Thread Tools

Parsing a string to get integers

 
 
Ram
Guest
Posts: n/a
 
      11-29-2010
Hi,

I have a string "1,2"
i want to parse it and get these two values in a seperate variable.

my main intention is to read these two values from a file, where these
values are programmable and changes by user menu.

I am new to java programming, please give suggest some coding
examples.

Thanks in advance!

-Ram
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      11-29-2010
On 28-11-2010 22:13, Ram wrote:
> I have a string "1,2"
> i want to parse it and get these two values in a seperate variable.
>
> my main intention is to read these two values from a file, where these
> values are programmable and changes by user menu.
>
> I am new to java programming, please give suggest some coding
> examples.


String split to convert one string to two strings.

Integer parseInt to convert each of the two strings
to int.

Arne
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      11-29-2010
On 11/28/2010 7:13 PM, Ram wrote:

> I have a string "1,2"
> i want to parse it and get these two values in a seperate variable.
>
> my main intention is to read these two values from a file, where these
> values are programmable and changes by user menu.



Also consider the Scanner class. It takes a file directly:

File file = ...;
Scanner scanner = new Scanner( file );

But you can test it just with a string as shown below in a quick
example. You will have to set the delimiter correctly to match what is
being used in your file:

Scanner scanner = new Scanner( "1,2" ).useDelimiter( ",");
int i = scanner.nextInt();
int j = scanner.nextInt();
System.out.println( i+" "+j );

Prints "1 2".

C.f.:

<http://download.oracle.com/javase/tutorial/essential/io/scanning.html>


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-02-2010
On Sun, 28 Nov 2010 19:13:40 -0800 (PST), Ram <>
wrote, quoted or indirectly quoted someone who said :

>I have a string "1,2"
>i want to parse it and get these two values in a seperate variable.
>
>my main intention is to read these two values from a file, where these
>values are programmable and changes by user menu.
>
>I am new to java programming, please give suggest some coding
>examples.


Split it first with regex split. trim, then convert String -> int.

see http://mindprod.com/jgloss/regex.html#SPLITTING
String.trim()
http://mindprod.com/applet/converter.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

In programming, and documenting programs, keep vocabulary consistent and precisely defined! Variation in vocabulary to relieve the tedium is for novels.
 
Reply With Quote
 
code learner code learner is offline
Junior Member
Join Date: Dec 2010
Posts: 19
 
      12-26-2010
Hi Ram,

to parse the value from string you can consider StringTokenizer also.

bye
learner
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parsing a sequence of integers Steven Woody Python 9 12-19-2008 05:01 PM
Parsing a string into integers using istringstream Bit Byte C++ 2 11-06-2006 11:17 PM
parsing a string into substring and integers monkeys paw C Programming 8 09-15-2006 03:49 PM
Parsing integers ptoum Java 2 11-15-2004 05:21 AM
extracting integers from string R Java 2 07-25-2003 03:47 AM



Advertisments