2016-11-29 18 views
2

我正在處理屬性文件。關於它,我有一個關鍵是「user_email」和值,我把它設置爲[email protected]我想獲得下一個遞增值9(當然是10),但它返回一個ASCII碼

現在,在我的代碼中,我希望電子郵件的值能夠在我的程序運行時進行迭代,因此[email protected]將會是[email protected]等等,並且從那裏讀取屬性中的電子郵件值文件並在我的程序中調用它。

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.但是,它返回'/'字符。我想運行後[email protected]toomeuser/@gmail.com

+4

如果您要替換單個字符,您如何期待'9'被替換爲「10」? – Eran

回答

2

你感到困惑charString。字符是單個字符,並且將永遠不能表示'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; 
+1

這個解決方案不會讓問題更進一步,當輸入是**[email protected] **時會中斷... –

+0

@PerHuss好的,是的。但這不是問題。問題不在於「如何找到切斷絃樂的地方」。這是「如何正確地替換數字」,我回答了。我確實期待一個後續問題,雖然 –

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("[email protected]"), is("[email protected]")); 
} 
+0

_「有些人在遇到問題時認爲 」我知道,我會用正則表達式。「現在他們有兩個問題。」_ [(Jamie Zawkinski)]( http://regex.info/blog/2006-09-15/247)。我不確定這需要一個正則表達式。 – davmac

+0

@davmac:也許你可以用沒有正則表達式用法的解決方案做出貢獻呢? ;-) –

+1

親自爲此,我會去做一些像Sylvain Boisse在他的回答中發佈的內容。爲了公平起見,我知道很多人會使用正則表達式來解決這樣的問題,傑米·扎文斯基也不會因爲公平的批評而出名。 :)我認爲雖然如果你對數字的位置有特殊的要求,但似乎是這樣,使用正則表達式只會引入破壞某些東西的機會(例如,在地址的前面有另一個數字,增加)。 – davmac

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碼和ASCII有可供1至9 ASCII代碼,所以在這種情況下,10將像1和O不同的字符,所以改爲使用int轉換爲int(在這種情況下,您不需要添加48,因爲我們直接處理字符串)

您可以參考以下鏈接: http://ascii.cl/

+0

當然'str.setCharAt(9,toInteger);'不會達到它需要的嗎?您需要改爲插入字符串「toString」。 – davmac

+0

Thnx指出編輯答案 –

+1

您將不得不使用一種方法接受一個字符串,並確保您替換舊字符... –

相關問題