473,412 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Extract Digits from a String

94 64KB
Hi, everyone. So, suppose I have a String variable called str, with the value "123". I also have an array of integers, with length 3. I am trying to put each digit in the array:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class test
  3. {
  4.     public static void main( String[] args )
  5.     {
  6.         int digits[] = new int[ 3 ];
  7.         String str = "123";
  8.  
  9.         /* 'Extract' digits: */
  10.         for ( int i = str.length() - 1 ; i >= 0 ; i-- )
  11.         {
  12.             digits[ i ] = ( int ) str.charAt( i );
  13.         }
  14.  
  15.         /* Print array: */
  16.         for ( int i = 0 ; i <= 2 ; i++ )
  17.         {
  18.             System.out.print( digits[ i ] );
  19.         }
  20.     }
  21. }
  22.  
  23.  
This isn't working, though. IfWhen I print the number, the value 495051 is displayed. What am I doing wrong?
Aug 14 '13 #1
3 3248
stdq
94 64KB
Solved it by changing line 12 to

Expand|Select|Wrap|Line Numbers
  1. digits[ i ] = Character.getNumericValue( str.charAt( i ) );
I think I was working with the ASCII and/or Unicode representations of 1, 2 and 3 instead.
Aug 14 '13 #2
Nepomuk
3,112 Expert 2GB
Great work! Also, thanks for sharing your solution. :-)
Aug 16 '13 #3
stdq
94 64KB
Thanks! You're welcome!
Aug 16 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lauren Quantrell | last post by:
Is there anyway to extract part of a string in a stored procedure using a parameter as the starting point? For example, my string might read: x234y01zx567y07zx541y04z My Parameter is an nvarchar...
1
by: mark | last post by:
How to extract digits from a given number say double number is 10.9876 extract two digits after decimal - 98 extract three digits after decimal - 987 and represent the extracted number as...
3
by: Lauren Quantrell | last post by:
This a a long convoluted string parsing question... I have a string in an Access 2K database table field that I use for noting if a user has checked a record. The string goes like this: xy So...
1
by: | last post by:
Hiya, I need to extract a string (ascii) from a buffer, my buffer is dim buf(1023) as byte This buffer containg an "array" of type xxx type xxx is defined in C as follows
1
by: nkg1234567 | last post by:
I'm trying to extract HTML from a website in the form of a string, and then I want to extract particular elements from the string using the substr function: here is some sample code that I have thus...
3
by: Cylix | last post by:
HI, I got an PDF with a number of pages. Each page has a table with the same format of data. Are there any method of component to extract the data(string) from the pdf? Thanks for any advise.
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
0
by: Sriram Rajan | last post by:
I am wondering if I can use re.search to extract from a particular location in a string. Example: string1='/Users/sriram/folder1/folder2/folder3/folder4/folder5/file' re.search ('folder3,string1)...
0
by: Timothy Grant | last post by:
On Mon, Aug 4, 2008 at 4:54 PM, Sriram Rajan <sriramrajan@acm.orgwrote: Something like below? Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:16) on darwin Type "help", "copyright", "credits"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.