2013-09-28 22 views
-2

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

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

什么错误?... –

回答

1

Scanner.next()将返回下一个标记(字)。可能,您想使用Scanner.nextLine()代替。

+1

'固定字符串= sbf.substring(0);'并不需要在那里。 'toString'就可以了。 – Rhys