Results 1 to 14 of 14
  1. #1
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Modifying a String of 10 digits

    Quote Originally Posted by ljd0991 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Modifying a String of 10 digits

    Quote Originally Posted by ljd0991 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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. #8
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: Modifying a String of 10 digits

    Sorry, didnt see your post.

  9. #9
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Modifying a String of 10 digits

    Quote Originally Posted by ljd0991 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Modifying a String of 10 digits

    Quote Originally Posted by ljd0991 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    ljd0991 is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Re: Modifying a String of 10 digits

    Will that way not work then mate?

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Modifying a String of 10 digits

    Quote Originally Posted by ljd0991 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  2. Modifying this app.
    By seaisle68 in forum Java Applets
    Replies: 5
    Last Post: 04-07-2011, 03:12 PM
  3. Modifying output
    By sqlsql0 in forum New To Java
    Replies: 1
    Last Post: 02-28-2011, 11:33 AM
  4. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  5. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM

Posting Permissions

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