Java中的字符串操作
这个代码正确吗?我在一些地方以评论的形式提到了我的疑问:Java中的字符串操作,java,Java,这个代码正确吗?我在一些地方以评论的形式提到了我的疑问: public class pract1 { public static void main (String[] args) { int i; String [] array = new String[20]; // Is this declaration correct? BufferedReader br = new BufferedReader(new InputStre
public class pract1
{
public static void main (String[] args)
{
int i;
String [] array = new String[20]; // Is this declaration correct?
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Array: ");
array = br.readLine(); // Is this the correct way to accept input from keyboard?
i=0;
while(array[i]!='\0') // Can I use the null pointer concept in Java?
{
System.out.println("The "+(i+1)+"character is:" +array[i]+"\n"); //Want to print each and every characters in string along with its position
i++;
}
}
}
Java字符串是一个对象,而不是数组。如果您对数组更熟悉,可以使用split来获取字符串数组。您应该注意,派生数组中的每个元素都是只有一个字母的字符串对象,而不是原语字符
String[] array = "abc".split("");
for(int i; i< array.length; i++)
{
System.out.println("The "+(i+1)+"character is:" +array[i]+"\n");
}
您可能想阅读这个问题来了解Java中字符串对象的概念
此声明对于字符串类型的20个插槽的数组是正确的,所有这些插槽都初始化为null。但您可能不需要立即使用,或者不需要以这种形式使用。
您不需要初始化变量,特别是如果您使用如下指令覆盖该初始化:array=
获取输入
是的,readLine是一条路要走,但是,正如编译器告诉您的,readLine;不返回字符串数组,而是返回单个字符串。应改用字符串变量:
// initialize a String variable 'line' containing the whole line without the '\n' at the end
String line = br.readLine();
更新:您也可以使用,这通常是我们所做的。然后,使用sc.nextLine获得与br.readLine类似的结果
打印字符
您可以通过charAtint方法访问字符串中给定位置的字符。此外,您不必处理复杂的C语言内容,例如通过“\0”查找字符串的结尾。您应该使用适当的for循环,如下所示:
String line = br.readLine();
for (int i = 0; i < line.length(); i++) {
System.out.println("The "+(i+1)+" character is: " + line.charAt(i) +"\n");
}
这是您也可以这样做的方式:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Pract1
{
public static void main (String[] args)
{
String array = ""; //initialize an empty string that will hold the value from the keyboard
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // initialize the BufferedReader
// while "stop" is not typed on the console, keep running
while(!array.equals("stop")) {
System.out.println("Enter the Array: ");
try {
array = br.readLine(); // read from the console
System.out.println("Array:" + array);
for(int i=0; i<array.length(); i++) { //loop through the input and show the chars
System.out.println("Character "+(i+1)+" is: " +array.charAt(i));
}
} catch (Exception e) { // catch any exception and show only the message, not the entire stackTrace
System.out.println("Error: " + e.getMessage());
}
System.out.println("");
}
}
}
你为什么不试着编译并运行你的代码呢?使用javac,编译器会告诉你是否有问题。只有您正在使用的null指针可能是一个问题,因为在java中它只是null。哦,访问数组[21]将导致ArrayIndexoutOfBoundsException;br.readLine返回一个字符串而不是数组。它看起来不错,但我没有编译它,所以请尝试这样做。可以肯定的是,当您运行该代码时,不会对您的计算机造成伤害。我想我肯定。我想你是从c语言来的。您应该阅读java基础知识。在这种情况下,您可以使用字符串对象,而不需要字符数组。Arun理解代码会更有帮助:Hi Joffrey,BufferedReader br=new BufferedReadernew InputStreamReaderSystem.in有任何错误吗;我只是试着用在线编译器编译。但它在BufferedReader中显示了错误。@Arun您不能在没有编译器的情况下尝试编写代码。您应该下载EclipseIDE,它以后可能会对您有所帮助。编译代码,您将看到语句中没有错误。此外,它还能工作。@Arun说,我们通常使用扫描仪获取用户输入。看看我的编辑。
System.out.println("The "+(i+1)+"character is:" +array[i]+"\n");
//Want to print each and every characters in string along with its position
String line = br.readLine();
for (int i = 0; i < line.length(); i++) {
System.out.println("The "+(i+1)+" character is: " + line.charAt(i) +"\n");
}
String line = br.readLine();
char[] chars = line.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println("The "+(i+1)+" character is: " + chars[i] +"\n");
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Pract1
{
public static void main (String[] args)
{
String array = ""; //initialize an empty string that will hold the value from the keyboard
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // initialize the BufferedReader
// while "stop" is not typed on the console, keep running
while(!array.equals("stop")) {
System.out.println("Enter the Array: ");
try {
array = br.readLine(); // read from the console
System.out.println("Array:" + array);
for(int i=0; i<array.length(); i++) { //loop through the input and show the chars
System.out.println("Character "+(i+1)+" is: " +array.charAt(i));
}
} catch (Exception e) { // catch any exception and show only the message, not the entire stackTrace
System.out.println("Error: " + e.getMessage());
}
System.out.println("");
}
}
}