2

我正在处理一个属性文件。在它上面,我有一个键“user_email”和值,我将它设置为toomeuser0@gmail.com

现在,在我的代码中,我希望在我的程序运行时迭代电子邮件值,因此 toomeuser0@gmail.com 将是 toomeuser1@gmail.com 等等,然后从那里读取属性文件中的电子邮件值并调用它在我的程序中。

 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;


    }

我知道这对你来说有点乱。但是当电子邮件编号字符达到值 9 然后它增加时,我预计它是 10。但是,它返回 '/' 字符。跑步后我想要的是toomeuser10@gmail.com 而不是toomeuser/@gmail.com

4

3 回答 3

2

如果您想支持大于 9 的数字,我建议使用正则表达式功能来查找和替换数字:

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"));
}
于 2016-11-29T09:24:04.753 回答
2

你很困惑charString。char 是单个字符,永远无法表示'10',因为这个数字是用两个 chars:1和编写的0。只有 aString可以正确表示数字 10,如下所示:"10"

所以这些行应该改变:

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

对于这样的事情(未经测试):

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;
于 2016-11-29T09:15:40.187 回答
1

而不是使用以下代码

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

用这个

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

由于 10 没有 ASCII 码,而 1 到 9 有 ASCII 码,所以在这种情况下,ASCII 中的 10 就像 1 和 O 不同的字符,所以改为使用 int 转换为 int (在这种情况下,您不需要在我们直接处理 String 时添加 48)

您可以参考以下链接: http ://ascii.cl/

于 2016-11-29T09:30:14.283 回答