Programming questions

Posted on 2006-05-22
Last Modified: 2010-05-18
I need to write a program where you enter a decimal number:


and the program returns the numbers in  front of the decimal point and the numbers after the decimal point so the out put would look like this

a= 1234
b= 1214

Question by:tkschultz1207
    LVL 86

    Assisted Solution

    Use String.split

    Author Comment

    How do you use String.split?
    LVL 86

    Expert Comment

    String tokens[] = "1234.1214".split("\\.");
    String a = tokens[0];
    String b = tokens[1];
    LVL 92

    Expert Comment

    Use parseInt() to parse the values

    int a = Integer.parseInt(tokens[0]);
    int b = Integer.parseInt(tokens[1]);

    You could also use a StringTokenizer

    String s = "1234.5678";
    StringTokenizer st = new StringTokenizer(s, ".");
    int a = Integer.parseInt(st.nextToken());
    int b = Integer.parseInt(st.nextToken());
    LVL 9

    Accepted Solution

    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split  method of String or the java.util.regex package instead.

    The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

             String[] result = "this is a test".split("\\s");
             for (int x=0; x<result.length; x++)
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    zeroFront challenge 7 58
    scores100 challenge 3 67
    wordsCount challenge 10 49
    nestparen challenge 4 34
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now