Results 1 to 14 of 14
Thread: Modifying a String of 10 digits
 10282011, 08:49 PM #1Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Modifying a String of 10 digits
Hi again.
suppose my program reads in a string from the user. The string represents a 10 digit codword
an example input would be:
0079584584
I now run a series of equations to determine if any character's need changing. possibilities are:
1) no characters need replacing
2) 1 character needs replacing
3) two characters need replacing
So supposed my equations determined that 1 character need replacing. say this character was the 7 in the above String of 10 digits and it needed replacing with a 9 to create a valid 10 digit codeword.
how do i go about this?
I have researched and i dont think the String.Replace() will work with this?
 10282011, 08:53 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
Re: Modifying a String of 10 digits
You can get the characters in a String with the toCharArray() method. You can later (after changing one or more characters in the array) build a String out of the char array again.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 10282011, 09:05 PM #3Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
okay so my String which reads in the 10 digits is called 'input'
I am trying your way so i have:
char[] arr = input.toCharArray();
I have an int 'i'  which represents the position of the error. so the digit that needs replacing is at index: (i1) because indexing starts at 0 obviously and 'i' only represents the position i.e. 1st digit, 2nd digit, 3rd digit, etc. rather than the actual index.
and i have an int called 'modified' which represents the new value that needs to be put in the string.
How do i now put 'modified' in at index (i1), replacing the original value
I hope this makes sense
 10282011, 09:11 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
 10282011, 09:18 PM #5Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
Are you still suggesting the toArrayChar way then?
Its just my Lewis and Loftus Java Software Solutions book doesnt have anything on this.
And i cant find anything online using toArrayChar() and replacing.
 10282011, 09:21 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
Re: Modifying a String of 10 digits
cenosillicaphobia: the fear for an empty beer glass
 10282011, 09:25 PM #7Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
My idea was create an array with the 10 integers
d1 = Integer.parseInt(String.valueOf(input.charAt(0)));
d2 = Integer.parseInt(String.valueOf(input.charAt(1)));
d3 = Integer.parseInt(String.valueOf(input.charAt(2)));
d4 = Integer.parseInt(String.valueOf(input.charAt(3)));
d5 = Integer.parseInt(String.valueOf(input.charAt(4)));
d6 = Integer.parseInt(String.valueOf(input.charAt(5)));
d7 = Integer.parseInt(String.valueOf(input.charAt(6)));
d8 = Integer.parseInt(String.valueOf(input.charAt(7)));
d9 = Integer.parseInt(String.valueOf(input.charAt(8)));
d10 = Integer.parseInt(String.valueOf(input.charAt(9)));
[]d = {d1, d2, d3, d4, d5, d6, d7, d8, d9, d10};
and replace one of those with the new value
but again there is not alot of information about on replacing values in arrays with new ones.
The ArrayList uses the simple .add() and .remove() but i think this going way off the point of my task.
 10282011, 09:27 PM #8Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
Sorry, didnt see your post.
 10282011, 09:45 PM #9Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
Im trying to replace it with an int and it seems your way requires a char value.
int modified = Integer.parseInt(String.valueOf(input.charAt(i1))) + a;
char[] c = input.toCharArray();
c[i1]= modified; // change it
input = new String(c); // build a new String
I am getting possible loss of precision, required: char dound : int
regards
 10282011, 09:48 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
 10282011, 09:52 PM #11Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
Thanks, i actually researched this but there are a lot of pages saying the converted int will just be the ASCII value rather than converting the int "1" to a char "1"
I will try this
 10282011, 09:55 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
Re: Modifying a String of 10 digits
cenosillicaphobia: the fear for an empty beer glass
 10282011, 10:21 PM #13Member
 Join Date
 Oct 2011
 Posts
 12
 Rep Power
 0
Re: Modifying a String of 10 digits
Will that way not work then mate?
 10282011, 10:24 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,787
 Blog Entries
 7
 Rep Power
 21
Re: Modifying a String of 10 digits
cenosillicaphobia: the fear for an empty beer glass
Similar Threads

Java help 25element array of digits to store integers as large as 25 digits
By CCC3 in forum New To JavaReplies: 3Last Post: 10272011, 06:23 PM 
Modifying this app.
By seaisle68 in forum Java AppletsReplies: 5Last Post: 04072011, 04:12 PM 
Modifying output
By sqlsql0 in forum New To JavaReplies: 1Last Post: 02282011, 12:33 PM 
Value should be 7 or 8 digits .If 8 digits, the last should be a character
By renu in forum New To JavaReplies: 1Last Post: 01192011, 10:23 PM 
Count number of digits in string using scanner
By wendysbiggy in forum New To JavaReplies: 35Last Post: 01202010, 06:11 AM
Bookmarks