2016-01-11 45 views
0

子字符串使用java中的流緩衝區類從字符串中刪除。 輸出來錯:使用java中的流緩衝區類從字符串中刪除子串字符串錯誤 - 驗證

enter image description here

請覈實該計劃

見我的輸出和解釋程序是什麼? 什麼是必要的輸出,以及程序中必須做些什麼改變才能獲得更正輸出?

import java.io.*; 
public class Strbuff 
{ 
    public static void main(String arg[])throws IOException 
    { 
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 
     String str; 
     try 
     { 
      System.out.println("Enter your name"); 
      str=in.readLine(); 
      str+="\nThis is example for string Buffer class and its function"; 
      StringBuffer strbuf=new StringBuffer(); 
      strbuf.append(str); 
      System.out.println(strbuf); 
      strbuf.delete(0,str.length()); 
      strbuf.append("hello"); 
      strbuf.insert(5,"-java"); 
      System.out.println(strbuf); 
      strbuf.reverse(); 
      System.out.println("Reversed string"); 
      System.out.println(strbuf); 
      strbuf.reverse(); 
      System.out.println(strbuf); 
      strbuf.setCharAt(5,' '); 
      System.out.println(strbuf); 
      System.out.println("Character at 6th position"); 
      System.out.println(strbuf.substring(3,7)); 
      strbuf.deleteCharAt(3); 
      System.out.println(strbuf); 
      System.out.println("Capacity of the string Buffer object"); 
      System.out.println(strbuf.capacity()); 
      strbuf.delete(6,strbuf.length()); 
      System.out.println("The string with first 6 letters"); 
      System.out.println(strbuf); 
     } 
     catch(StringIndexOutOfBoundsException e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 
+4

「什麼是必須的輸出以及在程序中必須進行哪些更改以獲得更正的輸出?」這是你的計劃:我們應該如何知道你想要做什麼? –

+0

你期望輸出什麼? – ParkerHalo

+0

其實,請解釋一下這個話題: 子串使用java中的流緩衝類去掉一個字符串 是我的o/p與它有關嗎? – Vikraman

回答

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

是...的意見是過度和古老但你用它做的時候,你會成爲一名職業與一段代碼。 ;)

相關問題