2

I was working on a property file. On it, I have an key which is "user_email" and the value, I set it to toomeuser0@gmail.com.

Now, in my code, I want the email value to iterate whenever my program runs, so toomeuser0@gmail.com will be toomeuser1@gmail.com and so on and from there, I read the email value in the property file and call it in my program.

 public String getEmailFromProperty(){

        String new_email = "";

        try{

            new ConfigReader();
            FileOutputStream fos = new FileOutputStream(property_file);
            StringBuilder str = new StringBuilder(property.getProperty("user_email"));

            char charWithNumber = str.charAt(9); // get the number character of the email (starts with 0)

            int toInteger = (int) charWithNumber; // convert that character number to an Integer value

            int numericValue = Character.getNumericValue(toInteger+1); // the number character in email is converted to int then incremented by 1 on each run

            int toAscii = numericValue + 48; // the number character (which is now an Int) is added by 48

            char toChar = (char) toAscii; // it is converted back to a character in order for it to be passed as a parameter to setCharAt() method

            str.setCharAt(9, toChar); // attached the newly incremented number character to the email @ 9th index

            new_email = str.toString(); // converted the StringBuilder variable str to an ordinary String in order to call toString() method

            property.setProperty("user_email", new_email); // now, I wrote the new email to the property file using the "user_email" key


            property.store(fos, null);
            fos.close();


        }
        catch(Exception e){
            System.out.println("Error is " + e.getMessage());

        }

        return new_email;


    }

I know this is kinda messy for you. But when the email number character reaches to value 9 and then it increments, I expected it to be 10. However, it returns '/' character. What I want after the run is toomeuser10@gmail.com NOT toomeuser/@gmail.com

Bharat
  • 2,441
  • 3
  • 24
  • 36
Jong Onin
  • 215
  • 1
  • 5
  • 12
  • 4
    If you are replacing a single character, how can you expect '9' to be replaced by "10"? – Eran Nov 29 '16 at 09:00

3 Answers3

2

You are confusing char and String. A char is a single character, and will never be able to represent '10', because this number is written with two chars: 1 and 0. Only a String can represent the number 10 properly, like so: "10"

So these lines should be changed:

int numericValue = Character.getNumericValue(toInteger+1); // <-- This breaks at '9'
int toAscii = numericValue + 48; // <-- This breaks after '9'
char toChar = (char) toAscii; // <-- this should be a String
str.setCharAt(9, toChar); // This cannote be used because we may need more than 1 char

To something like this (untested):

int numericValue = Character.getNumericValue(toInteger) + 1; // Note the parenthesis
String asString = String.valueOf(numericValue);
String prefix = str.subString(9);
String suffix = str.subString(10, str.length());
str = prefix + asString + suffix;
MrBrushy
  • 680
  • 3
  • 17
  • 1
    Won't this solution just take the problem one step further, and break when the input is **toomeuser10@gmail.com**... – Per Huss Nov 29 '16 at 10:12
  • @PerHuss Well, yes. But that's not the problem. The question was not "how to find where to cut the String". It was "how to replace the numbers properly" which i answered. I do expect a follow-up question though – MrBrushy Nov 29 '16 at 10:52
2

If you would like to support larger numbers than 9, I would recommend using regexp functionality to find and replace the number:

public String incrementNumberInEmail(String email)
{
    Matcher matcher = Pattern.compile("\\d+").matcher(email);
    if(matcher.find()) {
        StringBuffer buffer = new StringBuffer();
        int next = Integer.valueOf(matcher.group()) + 1;
        matcher.appendReplacement(buffer, String.valueOf(next));
        matcher.appendTail(buffer);
        return buffer.toString();
    }
    else throw new IllegalArgumentException("Email does not contain number: " 
                                            + email);
}

@Test
public void test()
{
    assertThat(incrementNumberInEmail("foo42@fubar.com"), is("foo43@fubar.com"));
}
Per Huss
  • 4,755
  • 12
  • 29
  • _"Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems."_ [(Jamie Zawkinski)](http://regex.info/blog/2006-09-15/247). I'm not sure this needs a regex. – davmac Nov 29 '16 at 09:56
  • @davmac: Perhaps you can contribute with a solution without regexp usage then? ;-) – Per Huss Nov 29 '16 at 10:09
  • 1
    Well personally I'd go for something like what Sylvain Boisse posted in his answer. To be fair I know a lot of people _would_ use a regex for a problem like this, and Jamie Zawinski is not known for even-handed criticisms. :) I think though that if you have specific requirements about the position of the number, as seems to be the case, using a regex only introduces the chance of breaking something (by eg having another number earlier in the address which is not supposed to be incremented). – davmac Nov 29 '16 at 10:16
1

Instead of using following code

int numericValue = Character.getNumericValue(toInteger+1); // the number character in email is converted to int then incremented by 1 on each run

    int toAscii = numericValue + 48; // the number character (which is now an Int) is added by 48

    char toChar = (char) toAscii; // it is converted back to a character in order for it to be passed as a parameter to setCharAt() method

    str.setCharAt(9, toChar); // attached the newly incremented number character to the email @ 9th index

Use this

toInteger = toInteger+1;
toString = toInteger + "";
str.setCharAt(9,toString);

As there is no ASCII code for 10 and there are ASCII code available for 1 to 9 so in this case 10 in ASCII would be like 1 and O different characters so instead use int convert to int (in this case you don't need to add 48 as we dealing with String directly)

you can refer following link : http://ascii.cl/

K patel
  • 54
  • 1
  • 5
  • Surely `str.setCharAt(9,toInteger);` will not achieve what it needs to? You would need to insert the string `toString` instead. – davmac Nov 29 '16 at 10:05
  • 1
    You will have to use a method that accepts a string, and make sure you replace the old character... – Per Huss Nov 29 '16 at 10:45