Reputation: 109
I am a student and kind of new to Java. For my homework I have to:
Ask the user to input a number (at least 7) using a do while loop.
Using a for loop I am required to ask the user to input that
number of words.
Then I have to check if one of the words fulfills the given conditions:
The word must:
I am asked to create a method inside some code homework that does a specific task, the method should check all the required conditions, the name of the method should be countTest
and it accepts the String as a parameter.
I will show you my code but I don't know how to create this specific method.
Output format
System.out.println("There as a total number of words " + count + " and
the ones that fulfill the condition are: " + condition);
The problem is, I dont know how to create the method or constructor or whatever it is called that calls all of the 3 methods inside it, and then connect that particular method to the main method! I hope you guys can understand I am new to this, thank you in advance!
public class D6_6 {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Type a number that is at least 7");
int number = sc.nextInt();
int count = 0;
int condition = 0;
do{
if(number<7){
System.out.println("You should type a number that is at least 7 or higher");
number = sc.nextInt();
}
}
while(number<7);
sc.nextLine();
String str;
for(int i =0; i<number; i++){
System.out.println("Type a word");
str = sc.nextLine();
count++;
}
}
public boolean countTest(String str) {
}```
Upvotes: 1
Views: 1083
Reputation: 109
I think I did it, thank you guys very much, I appreciate it!
public class D6_6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Type a number that is at least 7");
int number = sc.nextInt();
int count = 0;
int condition = 0;
do {
if (number < 7) {
System.out.println("You should type a number that is at least 7 or higher");
number = sc.nextInt();
}
}
while (number < 7);
sc.nextLine();
String str;
for (int i = 0; i < number; i++) {
System.out.println("Type a word");
str = sc.nextLine();
count++;
if((countTest(str))){
condition++;
}
}
if(count == 0){
System.out.println("No words typed");
} else {
System.out.println("Total number of words typed: " + count + ", which fulfill the condition: "+ condition);
}
}
public static boolean countTest(String str) {
return Character.isUpperCase(str.charAt(0)) && str.charAt(str.length() - 1) >= 48 || str.charAt(str.length() - 1) <= 57 || str.contains("cse");
}
}```
Upvotes: 0
Reputation: 46
To check if the word start with an uppercase:
You can do that by first selecting the character you want to check by str.charAt(0)
. This will return a char that is the first letter of the input str
.
To check if this char is an uppercase letter, you can easily use char.isUppercase()
. This will return a boolean. You have to replace char
by the name of the variable were you put the char of str.charAt(0)
in.
To check if the last character is a number:
You can do that again by first selecting the last character by str.charAt(str.length()-1)
, were string.length-1
is the number of the last character.
To check if this character is a number, you can use the ascii table. Every character has it's own number. So if you want to check if your character is between 0 and 9, you can use char >= 48 || char <= 57
(look up in the ascii table). Again, char
is the name of the variable were you put the char of str.charAt(str.length()-1)
in.
To check if the word contains "cse":
There is a very easy method for that: str.contains("cse")
will return a boolean that is true when "cse" is in the word and false when the word does not contain "cse".
I hope it is clear for you now!
Upvotes: 2