【问题标题】:Space in string causes error字符串中的空格导致错误
【发布时间】:2023-03-10 13:19:02
【问题描述】:

我正在尝试编写代码,允许我在给定索引处删除字符串的特定字符。但是,每当我在“原始”中使用空格时,都会出现一系列错误。 我有以下。

import java.util.*;
public class Test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a word");
        String raw = reader.next();
        System.out.println("Enter the number of the letter you would like to remove");
        int x = reader.nextInt();


        StringBuffer sbf = new StringBuffer(raw);
            sbf.deleteCharAt(x-1);
        String fixed = sbf.substring(0);

    System.out.println(fixed);
    }
}

【问题讨论】:

  • 什么错误?...

标签: java string space


【解决方案1】:

Scanner.next() 将返回下一个标记(单词)。可能,您想改用 Scanner.nextLine()。

【讨论】:

  • String fixed = sbf.substring(0); 也不需要在那里。 toString 可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 2014-04-30
相关资源
最近更新 更多