Scanner sc = new Scanner(System.in); StringBuffer str = new StringBuffer("We are happy."); //str = modify(str); for (int i =0;i<str.length();i++){ if (str.charAt(i) == ' '){ str.replace(i,0,"%20"); } } System.out.println(str);;为什么出错
时间: 2024-03-08 11:50:50 浏览: 20
这段代码出错的原因是 `str.replace(i, 0, "%20")` 的使用方式不正确。`replace()` 方法的第一个参数是要替换的字符串的起始位置,第二个参数是要替换的字符串的结束位置(不包括该位置),第三个参数是要替换成的新字符串。在这个代码中,第二个参数为 0,表示不替换任何字符,而第一个参数为 i,就会导致插入 `%20` 后字符串长度增加,从而超出原字符串的范围,出现越界访问的错误。
要想将空格替换成 `%20`,可以使用 `str.replace(i, i+1, "%20")`,这样就可以替换掉空格并保持字符串的长度不变。
修改后的代码如下:
```
Scanner sc = new Scanner(System.in);
StringBuffer str = new StringBuffer("We are happy.");
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
str.replace(i, i+1, "%20");
}
}
System.out.println(str);
```
相关问题
Scanner sc=new Scanner(System.in);作用
Scanner sc=new Scanner(System.in);是Java中用于读取用户输入的类。它的作用是创建一个Scanner对象,通过该对象可以获取用户在控制台输入的数据。
使用Scanner类可以实现以下功能:
1. 读取不同类型的输入:Scanner类提供了多种方法来读取不同类型的输入,例如nextInt()用于读取整数、nextDouble()用于读取浮点数、nextLine()用于读取字符串等。
2. 处理用户输入:通过Scanner类可以获取用户在控制台输入的数据,并将其存储到相应的变量中,以便后续的处理和计算。
3. 键盘输入:Scanner类可以从键盘获取用户输入的数据,使程序能够与用户进行交互。
使用示例:
```
Scanner sc = new Scanner(System.in); // 创建Scanner对象
System.out.print("请输入一个整数:");
int num = sc.nextInt(); // 读取用户输入的整数
System.out.println("您输入的整数是:" + num);
```
Scanner sc = new Scanner(System.in);怎么使用?
Scanner类是Java中的一个输入类,用于从控制台或文件中读取输入。使用Scanner类需要先导入java.util包。以下是Scanner类的使用示例:
1. 从控制台读取字符串:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符串: ");
String str = sc.nextLine();
System.out.println("输入的字符串是: " + str);
```
2. 从控制台读取整数:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入整数: ");
int num = sc.nextInt();
System.out.println("输入的整数是: " + num);
```
3. 从控制台读取浮点数:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入浮点数: ");
double num = sc.nextDouble();
System.out.println("输入的浮点数是: " + num);
```
4. 从控制台读取布尔值:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入布尔值(true/false): ");
boolean b = sc.nextBoolean();
System.out.println("输入的布尔值是: " + b);
```
5. 从控制台读取字符:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符: ");
char c = sc.next().charAt(0);
System.out.println("输入的字符是: " + c);
```
以上是Scanner类的基本使用方法,可以根据需求进行适当的变化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)