Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 替换扫描字符串中的字符_Java_String_Replace - Fatal编程技术网

Java 替换扫描字符串中的字符

Java 替换扫描字符串中的字符,java,string,replace,Java,String,Replace,因此,对于一个作业,我应该创建一个程序,将输入扫描仪的短语的每个字母加倍,每个感叹号加倍。以下是我到目前为止得到的信息: import java.util.Scanner; public class DoubleLetters{ public static void main(String[]args){ Scanner scan = new Scanner(System.in); System.out.println("Enter a statement"); St

因此,对于一个作业,我应该创建一个程序,将输入扫描仪的短语的每个字母加倍,每个感叹号加倍。以下是我到目前为止得到的信息:

import java.util.Scanner;
public class DoubleLetters{
  public static void main(String[]args){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a statement");
    String x = scan.nextLine();
    String y = x.replace("! ","!!! ");
    for(int j=0; j< x.length(); j++){
      System.out.print(y.charAt(j));
      System.out.print(y.charAt(j));
    }
  }
}
import java.util.Scanner;
公开类双字母{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入语句”);
字符串x=scan.nextLine();
字符串y=x。替换(“!”,“!!!”);
对于(int j=0;j
字母加倍有效,但感叹号不能加倍。我尝试使用替换方法,但它不起作用。非常感谢您的帮助。

试试这个

String y = x.replace("!","!!!");
还是这个

    for (char c : x.toCharArray()) {
        System.out.print(c);
        System.out.print(c);
        if (c == '!') {
            System.out.print(c);
        }
    }

将字符串转换为字符数组,而不是进行双行和三行并显示。从左侧开始读取,然后使用double和triplet条件更新字符数组移动到右侧,最后显示结果(字符数组)。

只需在循环中添加if语句即可

if(y.charAt(j) == '!') {
   System.out.print(y.charAt(j));
}
并删除替换方法中的空白

String y = x.replace("!","!!!");

您可以检查所有字符并将它们附加到
StringBuilder

String str = /* recieved from scanner */
StringBuidler builder = new StringBuilder();

for (char c : str.toCharArray()) {
    // Any character needs to be included at least once:
    builder.append(c);

    // If it's a letter, you need another copy (total of 2)
    if (Character.isLetter(c)) {
        builder.append(c);
    // If it's an excalmation mark, you need another two copies (total of 3)
    } else if (c == '!') {
        builder.append(c);
        builder.append(c); 
   }
}

System.out.println(builder);
系统输出打印(y.charAt(j)); 系统输出打印(y.charAt(j));
}

试试下面的代码,它会根据您的喜好高效工作

import java.util.Scanner;
public class DoubleLetters
{
   public static void main(String[]args)
   {
     Scanner scan = new Scanner(System.in);
     System.out.println("Enter a statement");
     String x = scan.nextLine();
     for(int j=0; j< x.length(); j++)
     {
   if(y.charAt(j)=='!')
    System.out.println("!!!");
   else
   {
          System.out.print(y.charAt(j));
          System.out.print(y.charAt(j));
   }
     }
   }
 }
import java.util.Scanner;
公开类双字母
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入语句”);
字符串x=scan.nextLine();
对于(int j=0;j
因为您尚未成功替换“!”。因此,对于替代替换的另一种方法,您可以使用StringTokenizer作为这段代码来标记字符串。这样你就可以在每个的末尾添加你想要的案例u可以再添加2个。代码为

System.out.println("---- Split by comma '!' ------");
    String s = "My Name !is !Manmohan"; //scanner string
    System.out.println("---- Split by comma ',' ------");
    StringTokenizer st2 = new StringTokenizer(s, "!");
    String now = "";
    while (st2.hasMoreElements()) {
        now += st2.nextElement() + "!!!";
    }

    System.out.println(now.substring(0, now.length() - 3));
            //op ...  My Name !!!is !!!Manmohan

我还不能使用数组,我的老师建议在某种程度上使用Character.isleter(ch)方法(虽然我不一定非得这么做),但我不知道我不允许以什么方式使用数组,只有条件句和我的教授建议使用以下方法(尽管我不一定非得这么做):Character.isleter(ch)但我不知道用什么方法