当我尝试打印像" Koushik Gupta"到" K.古普塔"使用以下字符串方法:
import java.io.*;
class Fndwrd
{
public static void main(String args[])
throws IOException
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,sn;
System.out.println("Enter the Sentence/string");
sn=" ";
s=br.readLine();
int p,l=s.length();
p=0;
for(int i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
sn=sn+' '+s.charAt(p);
p=i+1;
}
}
sn=sn+"."+s.substring(p);
System.out.println("Short name\n "+sn);
}
我收到以下错误:
答案 0 :(得分:0)
您缺少主要功能的开始和结束括号。
以下是更正后的版本,
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s, sn;
System.out.println("Enter the Sentence/string");
sn = " ";
s = br.readLine();
int p, l = s.length();
p = 0;
for (int i = 0; i < l; i++) {
if (s.charAt(i) == ' ') {
sn = sn + ' ' + s.charAt(p);
p = i + 1;
}
}
sn = sn + "." + s.substring(p);
System.out.println("Short name\n " + sn);
}
答案 1 :(得分:0)
请在程序中为main方法添加左右花括号。然后它会正常工作。否则请使用以下代码: -
import java.io.*;
class Fndwrd
{
public static void main(String args[]) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,sn;
System.out.println("Enter the Sentence/string");
sn=" ";
s=br.readLine();
int p,l=s.length();
p=0;
for(int i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
sn=sn+' '+s.charAt(p);
p=i+1;
}
}
sn=sn+"."+s.substring(p);
System.out.println("Short name\n "+sn);
}
}
希望它会对你有所帮助......谢谢