# Thread: Modifying a String of 10 digits

1. Member
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.

I have researched and i dont think the String.Replace() will work with this?

2. ## 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,

Jos

3. Member
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: (i-1) 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 (i-1), replacing the original value

I hope this makes sense

4. ## Re: Modifying a String of 10 digits

Originally Posted by ljd0991
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: (i-1) 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 (i-1), replacing the original value

I hope this makes sense
That is elementary programming; read any textbook on Java programming (it should be somewhere in the first chapters).

kind regards,

Jos

5. Member
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.

6. ## Re: Modifying a String of 10 digits

Originally Posted by ljd0991
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.
Throw that book away then, it lacks the explanation of the most simple Java stuff:

Java Code:
```char[] c= s.toCharArray();
int i= ...; // the position of the char to change
c[i]= ...; // change it
s= new String(c); // build a new String```
kind regards,

Jos

7. Member
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.

8. Member
Join Date
Oct 2011
Posts
12
Rep Power
0

9. Member
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(i-1))) + a;
char[] c = input.toCharArray();
c[i-1]= modified; // change it
input = new String(c); // build a new String

I am getting possible loss of precision, required: char dound : int

regards

10. ## Re: Modifying a String of 10 digits

Originally Posted by ljd0991
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(i-1))) + a;
char[] c = input.toCharArray();
c[i-1]= modified; // change it
input = new String(c); // build a new String

I am getting possible loss of precision, required: char dound : int
Yup, a char array can only store chars as its elements, so you should cast your int value to a char type:

Java Code:
`c[i-1]= (char)modified; // change it`
kind regards,

Jos

11. Member
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

12. ## Re: Modifying a String of 10 digits

Originally Posted by ljd0991
Thanks, i actually researched this but there are a lot of pages saying the int will just be the ASCII value of the char rather than converting the int "1" to a char "1"

I will try this
Hold on for a second, you started this thread claiming that one char needs to be replaced with another char; that's exactly what my method does; all the rest are details ;-)

kind regards,

Jos

13. Member
Join Date
Oct 2011
Posts
12
Rep Power
0

## Re: Modifying a String of 10 digits

Will that way not work then mate?

14. ## Re: Modifying a String of 10 digits

Originally Posted by ljd0991
Will that way not work then mate?
Sure, pay close attention:

Java Code:
```String s= "123";
char[] c= s.toCharArray();
c[2]= '9';
s= new String(c);
System.out.println(s);```
Play a bit with this bit of code and see for yourself.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•