【发布时间】:2020-07-19 21:47:41
【问题描述】:
正如标题所说,我想删除所有空格和所有不是 A-Z 的字符。如果有小写字符,则将它们转换为大写。 我想要的输出是:“THISISANEXAMPLEOUTPUTWITHONLYUPPERCASELETTER” 如何修复我的代码?
```
Here is what I have for the output right now:
THISISANEX
AMPLEOUTPUT
WITH
ONL
Y
UPPERCASELE
TT
E
R
123
```
---------示例文件------------
This is an Ex
amPle outP&ut
. WiTH
On l
Y
Uppercase Le
@
Tt
E
R
!!!!
123
^
我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner file = new Scanner(new File("Example.txt"));
while (file.hasNextLine()) {
String input = file.nextLine();
if (!input.isEmpty()) {
String res = input.toUpperCase().replaceAll("\\P{Alnum}", "");
System.out.println(res);
}
}
}
}
【问题讨论】:
标签: java file java.util.scanner uppercase replaceall