在java输出中使用流缓冲区类从字符串中删除子字符串错误验证
在java中使用流缓冲类从字符串中删除子字符串。 输出出错: 请验证此程序 查看我的输出并解释程序的内容?在java输出中使用流缓冲区类从字符串中删除子字符串错误验证,java,Java,在java中使用流缓冲类从字符串中删除子字符串。 输出出错: 请验证此程序 查看我的输出并解释程序的内容? 所需的输出必须是什么?为了获得正确的输出,必须对程序进行哪些更改 import java.io.*; public class Strbuff { public static void main(String arg[])throws IOException { BufferedReader in=new BufferedReader(new InputS
所需的输出必须是什么?为了获得正确的输出,必须对程序进行哪些更改
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());
}
}
}
这是基本的初学者资料,您必须已经涵盖并接触过允许您建立此代码功能的方法。至少你已经获得了执行要求任务所需的阅读材料(或位置) 你所需要做的就是按要求去做。相反,你认为你会来到斯塔奇科弗流,让别人为你做功课,而这不是论坛的目的。我们在这里帮助您克服在成为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());
}
是的……注释过多且过时,但当您完成注释时,您将成为该代码的专业人员。;) “需要什么样的输出,程序中必须做什么更改才能得到正确的输出?”这是你的程序:我们如何知道你想要它做什么?你期望得到什么输出?实际上,请解释一下这个主题:在java中使用流缓冲类从字符串中删除子字符串,我的o/p与之相关吗??