我想获得下一个增量值9(当然是10个),但它返回ASCII代码



我正在处理属性文件。在上面,我有一个键是" user_email",并且值将其设置为 tomeuser0@gmail.com

现在,在我的代码中,我希望每当我的程序运行时都需要迭代,因此tomeuser0@gmail.com将是tomeuser1@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。但是,它返回'/'字符。运行后我想要的是 tomeuser10@gmail.com not toomeuser/@gmail.com

您正在混淆charString。char是A 单个字符,并且永远无法代表'10',因为此数字是用两个 chars10编写的。只有String可以正确表示数字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;

如果您想支持大于9的数字,我建议使用RegeXP功能查找和替换数字:

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"));
}

,而不是使用以下代码

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中的ASCII代码就像1和o不同的字符,因此请使用int convert to int(在这种情况下,您可以不''当我们直接处理字符串时,需要添加48个)

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

相关内容

最新更新