My code takes a user inputted string and returns the number of words as well as the first word. When the user inputs an empty string I don't want "Your string has x words in it" or "The first word is x" to display so I created a boolean
but the boolean
is not being set within the method I tried to set it to true
in. Any Help I can get on why or how to fix it would be great. Thanks!
public static void main(String[] args){
boolean empty = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.nextLine();
if (empty == false) {
System.out.println("Your string has " + getWordCount(str)+" words in it.");
System.out.println("The first word is: " + firstWord(str));
}
}
public static String firstWord(String input) {
for(int i = 0; i < input.length(); i++)
{
if(input.charAt(i) == ' ')
{
return input.substring(0, i);
}
}
return input;
}
public static int getWordCount(String str){
int count = 0;
if(str != null && str.length() == 0){
System.out.println("ERROR - string must not be empty.");
empty = true;
}
else if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))){
for (int i = 0; i < str.length(); i++){
if (str.charAt(i) == ' '){
count++;
}
}
count = count + 1;
}
return count;
}
}
You have to re-think your logic here (see below snippet):
empty
variable getWordCount
method and store the result in a variable ( wordCount
?). Then you can check if there is at least one word by doing wordCount > 0
. The snippet:
public static void main(String[] args){
// boolean empty = false; // --> not needed
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.nextLine();
final int wordCount = getWordCount(str);
if (wordCount > 0) { // show message only if there is at least one word
System.out.println("Your string has " + wordCount +" words in it.");
System.out.println("The first word is: " + firstWord(str));
}
}
public static String firstWord(String input) {
// .. code omitted for brevity
}
public static int getWordCount(String str){
int count = 0;
if(str != null && str.length() == 0){
System.out.println("ERROR - string must not be empty.");
// empty = true; --> not needed
}
// ... code omitted for brevity
return count;
}
you just need to change the place if for the sake of variable visibility.
public static void main(String[] args) {
boolean empty = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.nextLine();
if (str != null && str.length() == 0) {
System.out.println("ERROR - string must not be empty.");
empty = true;
}
if (empty == false) {
System.out.println("Your string has " + getWordCount(str) + " words in it.");
System.out.println("The first word is: " + firstWord(str));
}
}
public static String firstWord(String input) {
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ' ') {
return input.substring(0, i);
}
}
return input;
}
public static int getWordCount(String str) {
int count = 0;
if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
count++;
}
}
count = count + 1;
}
return count;
}
如果您真的想使用布尔值,请尝试使用字符串参数创建一个布尔方法,该方法仅在字符串不为空时才返回true,然后将布尔值返回类型分配给empty
布尔值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.