I want to count the entries(Strings) written in console that end in "im", so how can I do it as a starter?
import java.lang.String;
import java.util.Scanner;
public class WordCount {
public static void main (String[]args){
final String SENTINEL = "END";
Scanner sc = new Scanner(System.in);
String text = "xy";
do {
System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
text = sc.nextLine();
} while(!text.equalsIgnoreCase(SENTINEL));
boolean IMcheck = text.endsWith("im");
int count = 0;
if(IMcheck == true){
count++;
}
System.out.println("You have typed "+ count +" texts that end in \"im\" ");
}
}
As the commenters have said, you need to move your if-statement inside the do-while loop. Having the variable "IMcheck" is unnecessary here.
To solve the issue, I've put the code you used to assign the "IMcheck" variable inside your if-statement, and I've moved it into your do-while loop.
public class WordCount {
public static void main (String[]args) {
final String SENTINEL = "END";
int count = 0;
Scanner sc = new Scanner(System.in);
String text = "xy";
do {
if(text.endsWith("im"))
count++;
System.out.print("Type a text or type "+SENTINEL+" when you are done. ");
text = sc.nextLine();
} while(!text.equalsIgnoreCase(SENTINEL));
System.out.println("You have typed "+ count +" texts that end in \"im\" ");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With