[英]Java boolean not being set to true
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. 当用户输入一个空字符串时,我不想显示“您的字符串中包含x个单词”或“第一个单词是x”,因此我创建了一个
boolean
但是未在我尝试设置的方法中设置boolean
它可以true
。我能获得有关为什么或如何修复它的任何帮助都将非常有用。 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 empty
变量 getWordCount
method and store the result in a variable ( wordCount
?). getWordCount
方法来知道“ word”是否为空并将结果存储在变量中( wordCount
?)。 Then you can check if there is at least one word by doing wordCount > 0
. 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
布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.