【问题标题】:Replacing a character in a scanner string替换扫描仪字符串中的字符
【发布时间】:2014-02-15 09:11:01
【问题描述】:

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

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));
    }
  }
}

字母加倍有效,但感叹号不加倍。我尝试使用替换方法,但它不起作用。请,任何帮助表示赞赏。

【问题讨论】:

    标签: java string replace


    【解决方案1】:

    只需在循环中添加 if 语句

    if(y.charAt(j) == '!') {
       System.out.print(y.charAt(j));
    }
    

    并删除替换方法中的空格

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

    【讨论】:

      【解决方案2】:

      试试这个

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

      或者这个

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

      【讨论】:

      • 我还不应该能够使用数组,我的老师建议以某种方式使用 Character.isLetter(ch) 方法(尽管我不一定必须)但我不知道以什么方式
      【解决方案3】:

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

      【讨论】:

      • 我不认为我被允许使用数组,只有条件和我的教授建议使用以下方法(虽然我不必): Character.isLetter(ch) 但我不知道用什么方法
      【解决方案4】:

      您可以遍历所有字符并将它们附加到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);
      

      System.out.print(y.charAt(j)); System.out.print(y.charAt(j)); }

      【讨论】:

        【解决方案5】:

        试试下面的代码,它会随心所欲地高效工作,

        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));
           }
             }
           }
         }
        

        【讨论】:

          【解决方案6】:

          因为你没有成功替换 '!' .因此,对于另一种方法而不是替换,您可以使用 StringTokenizer 作为这段代码来标记您的字符串。这样你就可以在每个 ! 的末尾添加你想要的,你可以再添加 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-23
            • 1970-01-01
            • 1970-01-01
            • 2012-05-21
            • 1970-01-01
            相关资源
            最近更新 更多