Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

substring help

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
   1
   2
   3
   4
   5

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

thanks!
0
azcalv408
Asked:
azcalv408
1 Solution
 
BogoJokerCommented:
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));
          System.out.println(num);
        }

There ya go!
Joe P
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now