Parsing a string to get integers

Discussion in 'Java' started by Ram, Nov 29, 2010.

  1. Ram

    Ram Guest

    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
     
    Ram, Nov 29, 2010
    #1
    1. Advertising

  2. Ram

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Nov 29, 2010
    #2
    1. Advertising

  3. Ram

    markspace Guest

    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>
     
    markspace, Nov 29, 2010
    #3
  4. Ram

    Roedy Green Guest

    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.
     
    Roedy Green, Dec 2, 2010
    #4
  5. Ram

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19
    Hi Ram,

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

    bye
    learner
     
    code learner, Dec 26, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ptoum

    Parsing integers

    ptoum, Nov 14, 2004, in forum: Java
    Replies:
    2
    Views:
    563
    marcus
    Nov 15, 2004
  2. Replies:
    9
    Views:
    528
    stathis gotsis
    Apr 13, 2006
  3. monkeys paw

    parsing a string into substring and integers

    monkeys paw, Sep 14, 2006, in forum: C Programming
    Replies:
    8
    Views:
    448
    Frederick Gotham
    Sep 15, 2006
  4. Bit Byte
    Replies:
    2
    Views:
    558
    Default User
    Nov 6, 2006
  5. Steven Woody

    How to parsing a sequence of integers

    Steven Woody, Dec 19, 2008, in forum: Python
    Replies:
    9
    Views:
    248
    Joe Strout
    Dec 19, 2008
Loading...

Share This Page