Parsing a string to get integers

R

Ram

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
 
A

Arne Vajhøj

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
 
M

markspace

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>
 
R

Roedy Green

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
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top