java规范了加密和解密文本的文本编写方法
我正在执行以下任务:
编写一个名为normalizeText()的方法,该方法执行以下操作:
-Removes all the spaces from your text
-Remove any punctuation (. , : ; ’ ” ! ? ( ) )
-Turn all lower-case letters into upper-case letters
-Return the result.
调用normalizeText("This is some \"really\" great. (Text)!?"
应该返回"THISISSOMEREALLYGREATTEXT"
以下是我的想法:
import java.util.Scanner;
public class Crypto {
public static void main(String[] args) {
normalizeText();
}
public static String normalizeText() {
Scanner input = new Scanner(System.in);
System.out.println("Insert your Text : ");
String crypto = input.next();
crypto.replace("\\s", "");
crypto.replaceAll("\\p{Punct}", "");
crypto.toUpperCase();
System.out.println(crypto);
return crypto;
}
}
对于我输入的每个句子,我只打印出句子的第一个单词,不需要进一步的字符串更改(插入文本:"asdfg asdf asd"
,返回"asdfg"
)。正如你所看到的,我是一个初学者,以提示的形式寻求帮助
# 1 楼答案
您需要使用
nextLine()
而不是next()
方法,因为next
方法只读取句子的第一个单词,这就是您在输出中看到第一个单词的原因。有关详细信息,请阅读this link关于删除空格和标点符号的通缉任务,我强烈建议您使用Regex