这段代码是否正确?我在某些地方以评论的形式提到了我的怀疑:
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++;
}
}
}
答案 0 :(得分:0)
Java字符串是一个对象,而不是数组。如果您对数组更熟悉,请使用split()来获取String数组。您应该注意,派生数组中的每个元素都是String Object,只有一个字母,而不是原始Char。
String[] array = "abc".split("");
for(int i; i< array.length; i++)
{
System.out.println("The "+(i+1)+"character is:" +array[i]+"\n");
}
您可能希望阅读本课题以了解Java Difference between "char" and "String" in Java
中的String Object答案 1 :(得分:0)
String [] array = new String[20]; // Is this declaration correct?
此声明对于包含String类型的20个插槽的数组是正确的(所有这些插槽都初始化为null
)。但你可能不会立即需要这个,或者不是这种形式。
您不需要初始化变量,尤其是在您通过以下指令覆盖初始化时:array = ...
。
array = br.readLine(); // Is this the correct way to accept input from keyboard?
是的,readLine()
是要走的路,但是as the doc states,正如编译器告诉你的那样,readLine();
不会返回String
的数组但是一个String
。您应该使用String变量:
// initialize a String variable 'line' containing the whole line without the '\n' at the end
String line = br.readLine();
更新:您也可以使用the Scanner
class,而这通常就是我们所做的。然后,使用sc.nextLine()
获得与br.readLine()
,as stated there类似的结果。
System.out.println("The "+(i+1)+"character is:" +array[i]+"\n");
//Want to print each and every characters in string along with its position
您可以通过方法charAt(int)
访问String中给定位置的字符。此外,您不必处理复杂的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");
}
另一种解决方案是使用the toCharArray()
method:
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");
}
答案 2 :(得分:0)
你也可以这样做:
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("");
}
}
}