我尝试接受用户输入的字符串,然后在几个新方法中修改它,以便稍后在程序中调用main。目前这段代码不起作用,我非常感谢一些帮助让它工作或者正确的方法,以便在程序后期将这些方法的返回值调用到main中。
import java.util.Scanner;
public class TextAnalyzer {
/*takes the user input and counts total characters to be called later.*/
public static int getNumOfCharacters(int textLength) {
Scanner scnr = new Scanner(System.in);
String usrText = "";
usrText = scnr.nextLine();
textLength = usrText.length();
return textLength;
}
/*removes all white space from string to be called later.*/
public static String outputWithoutWhitespace(String noWhiteSpace) {
Scanner scnr = new Scanner(System.in);
String usrText = "";
usrText = scnr.nextLine();
noWhiteSpace = usrText.replaceAll("\\s+","");
return noWhiteSpace;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String usrText = "";
System.out.print("Enter a sentence or phrase: ");
usrText = scnr.nextLine();
System.out.println("You entered: " + usrText);
System.out.println();
System.out.print("Number of characters: ");
System.out.println(getNumofCharacters(usrText));
System.out.print("String with no whitespace: ");
System.out.println(outputWithoutWhitespace(usrText));
}
}
答案 0 :(得分:0)
我在调用错误的东西时遇到了一些问题,而且没有匹配方法名称的格式。这是我用的。谢谢!
import java.util.Scanner;
public class TextAnalyzer {
public static int getNumOfCharacters(String usrText) {
int textLength;
textLength = usrText.length();
return textLength;
}
public static String outputWithoutWhitespace(String usrText) {
String noWhiteSpace = "";
noWhiteSpace = usrText.replaceAll("\\s+","");
return noWhiteSpace;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String usrText = "";
System.out.println("Enter a sentence or phrase: ");
usrText = scnr.nextLine();
System.out.println("You entered: " + usrText);
System.out.println();
System.out.print("Number of characters: ");
System.out.println(getNumOfCharacters(usrText));
System.out.print("String with no whitespace: ");
System.out.println(outputWithoutWhitespace(usrText));
return;
}
}