用文本末尾的单词替换文本文档中的整行

时间:2015-06-08 22:04:22

标签: java

我正在尝试将该行替换为该行末尾的单词。站在现在,我正在走同样的路线。

简单:

apple - water- wall - street- light- book

REsult应

book

代码:

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    if (line.contains("-") ) {                   
        String[] lineSplitted = line.split("-");
        int index = lineSplitted.length - 1;
        String direction = lineSplitted[index];
        line.replace(line, direction);
    }   
}

2 个答案:

答案 0 :(得分:1)

如果您要完成的是输入行,该行被替换为该行中的最后一个单词,您只需将代码更改为以下内容:

Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); String[] parts = line.split("\\s"); line = parts[parts.length-1];

答案 1 :(得分:0)

类型@Override public void onCompleted( final JSONObject object, GraphResponse response) { try { Toast.makeText(getApplicationContext(), "User Name is --" + object.get("name"), Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "User ID is --" + object.get("id"), Toast.LENGTH_LONG).show(); ProfilePictureView profilePictureView = new ProfilePictureView(getApplicationContext()); intentHomeActivity.putExtra(Constants.FACEBOOK_USER_OBJECT, object.get("id").toString()); startActivity(intentHomeActivity); } catch (JSONException e) { e.printStackTrace(); } } 是不可变的,这意味着一旦构造,对象本身就不能被更改。不要尝试更新字符串本身,而是更新引用(String)。