使用java-Output中的流缓冲类从字符串中删除子串错误 - 验证

时间:2016-01-11 13:12:34

标签: java

使用java中的流缓冲类从字符串中删除子串。 输出出错:

enter image description here

请验证此程序

查看我的输出并解释该程序的内容? 什么必须是所需的输出以及必须在程序中进行哪些更改以获得corrct输出?

for($i=0;$i<count($dados_atividades)-1;$i++)
{
        $arr[$i+1]['Descricao'] = $dados_atividades[$i]['Descricao'];
        $arr[$i+1]['DataInicioPrevista'] = $dados_atividades[$i]['DataInicioPrevista'];
        $arr[$i+1]['DataConclusaoPrevista'] = $dados_atividades[$i]['DataConclusaoPrevista'];
    }
}

if(count($arr) == 0) {
    $arr = array();
    $arr[]['Descricao'] = 'N/A';
}

$json_string = json_encode($arr,JSON_UNESCAPED_UNICODE);

if ( json_last_error() > 0 ) {
    file_put_contents('json_debug.txt', json_last_error_msg() );
} else {
    echo $json_string;
}
exit;

1 个答案:

答案 0 :(得分:0)

这是基本的初学者材料,您必须已经涵盖并触及了允许您确定此代码的功能的方法。至少,您将获得执行所请求任务所需的阅读材料(或位置)。

你需要做的就是做你所要求的事情。相反,你以为你会来StatckOverflow并让别人为你做功课,这不是论坛的用途。我们在这里帮助您解决在您成为Java程序员的过程中绊倒的绊脚石,但是,您必须至少表现出在您跌跌撞撞时尝试自我。复制/粘贴您的家庭作业代码不会被视为尝试。

逐步执行每行代码并放置一条注释,其中包含该行在每一行上的内容的简要说明。如果您不知道代码行的作用,那么请阅读您遇到问题的方法。尝试使用该方法进行不同的操作,在您了解之前,您将对此有充分的了解。

我会帮助您入门:

// Open a BufferReader InputStream and establish it as variable 'in'... 
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

// Declare a String variable named 'str'. This string is not originally
//initialized so I'm going to do that now with a Null String...
String str = "";

// Establish a try/catch block to trap a StringIndexOutOfBoundsException
// should it ever occur. This exception is thrown by String methods to
// indicate that an index is either negative or greater than the size of
// the string. For some methods such as the charAt method, this exception
// also is thrown when the index is equal to the size of the string. We
// want to catch this exception so it doesn't crash our program...
try {

    // Print to the display console for User to enter his/her name...
    System.out.println("Enter your name");

    // Read what the User supplied and place the text into our 'str' 
    // String variable...
    str=in.readLine();

    // Add (append) the string "\nThis is example for string Buffer class 
    // and its function" to the 'str' string variable. The '\n' is a output
    // string tag for establishing a new line within the displayed output.
    // the '+=' attached to our 'str' variable is a Assignment Operator. It
    // tells the compiler to adds the right operand to the left operand and
    // assign the result to left operand. It is the same as writing:
    // str = str + "\nThis is example for string Buffer class and its function";
    str+="\nThis is example for string Buffer class and its function";

    // Establish a String Buffer and name it 'strbuf. A StringBuffer is a 
    // thread-safe, mutable sequence of characters. A string buffer is like a 
    // String, but can be modified...
    StringBuffer strbuf=new StringBuffer();

    // Place our current string held within the 'str' string variable into our
    // new String Buffer (strbuf). The StringBuffer.append() method is used for
    // this...
    strbuf.append(str);

    // Print the contents within out string buffer (strbuf) to the display 
    // console...
    System.out.println(strbuf);

    // Now Delete (clear) everything within our String Buffer variable (strbuf).
    // This is done using the StringBuffer.delete() method. 0 indicates the starting
    // point index for the number of characters within the string we want to delete.
    // The 'str.length()' portion indicates the ending index point to delete. The 
    // 'str.length()' method always returns the length of (number of characters) within
    // the string variable (str). So, we know our string variable 'str' contains:
    // the User provided name, let's say: "Vikraman" plus "\nThis is example for string 
    // Buffer class and its function" which in total is 66 characters in length but
    // because indexing always starts from 0 the length of the string (str) is 65. Our
    // line below could be written strbuff.delete(0, 65). Delete characters within the
    // string buffer variable (strbuf) starting from index 0 (the first character) to 
    // index 65 (the last character).
    strbuf.delete(0,str.length());

    ....................................
    ....................................
    ........  You Do The Rest  .........
    ....................................
    ....................................

}
catch(StringIndexOutOfBoundsException e) {
    // Display the exception message to console.
    System.out.println(e.getMessage());
}

是的......这些评论过于陈旧,但是当你完成它时,你将成为一名专业人士。 ;)