substring help

Posted on 2006-04-22
Last Modified: 2010-03-31
Hi guys,
    How do I use substring function to extract 1 character at a time for a string of digits and convert it to int? ex: String s = "12345" ; I want to output

int i;
while( i < s.length() )
   int num = Integer.parseInt(substring(0,i));  // only get the first number

Question by:azcalv408
    1 Comment
    LVL 17

    Accepted Solution

    Hi azcalv408,

    Essentially a string is an array of characters.  So you could go character by character but I will stick to your example and modify it only slightly.

            String s = "12345";
            for (int i = 0; i < s.length(); i++)
              int num = Integer.parseInt(s.substring(i,i+1));

    There ya go!
    Joe P

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now