- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 23
Lesson 23: How to use equals() and charAt() methods in Java
Welcome to Lesson 23 of this continuing series on Programming in Java NetBeans. In Lesson 20, we discussed the compareTo() method in Java and how we can use this method to compare strings.
In this particular article, we shall look at a similar method to compareTo() that can be used to compare strings, that is the equals() method. We shall also look at another Java method, charAt() method, that can be used to return the character at a given string position.
How to use equals() method in Java
The Java equals() method is used to compare two string and returns a Boolean “true” value if the two strings are equal, otherwise it returns a Boolean “false” value.
To practice on the use of this method, we are going to write a simple quiz program that will check if the answer entered by user is the correct one.
We are going to select an option among the provided choices as the answer. The program, using the equals() method, will compare the entered string with the correct one and check if the two strings are the same.
The program then displays an appropriate message at the end of every question and the final score at the end of the quiz. Notice the use of IF… ElSE statement and the count variable.
if(response.equals("answer")){
count = count+1;
JOptionPane.showMessageDialog( null,"Correct! 1 score" );
}else{
JOptionPane.showMessageDialog( null,"Not correct! 0 score" );
}
Create a new Java class. Call it Quiz or any other preferred name. You can either type the code shown below (for the purpose of code practice) or just copy and paste. Run the program and see if it works as expected.
Java program using equals() method
package myfirstprogram; import javax.swing.JOptionPane; public class Quiz { public static void main(String[] args) { String response = ""; int count = 0; //Variable to store the score response = JOptionPane.showInputDialog("Which one is Kenyan capital city? Kampala, Nairobi"); //Use equals() method to compare answers from the user if(response.equals("Nairobi")){ count = count+1; //If correct, increment score by 1 JOptionPane.showMessageDialog( null,"Correct! 1 score" ); }else{ JOptionPane.showMessageDialog( null,"Not correct! 0 score" ); } response = JOptionPane.showInputDialog("Nelson Mandera is from? South Africa, Kenya"); if(response.equals("South Africa")){ count = count+1; //If correct, increment score by 1 JOptionPane.showMessageDialog( null,"Correct! 1 score" ); }else{ JOptionPane.showMessageDialog( null,"Not correct! 0 score" ); } //Display final score tally JOptionPane.showMessageDialog( null,"End of quiz. Total score = "+count); } }
Exercise: Modify the program so that it will have at least ten questions for the user.
How to use charAt() method in Java
The Java charAt() method is used to check which single character is in a particular position in a given string. When a string is passed to charAt() method, the method count from 0 and returns a digit number where the character was found. Let us write another simple Java program to test the use of charAt() method. Copy and paste or type the following program code and run.
Java program using charAt() method
package myfirstprogram; import javax.swing.JOptionPane; public class charAt { public static void main(String[] args) { String response = "Y/N"; response = JOptionPane.showInputDialog("Have you joined Hubpages.com? Y/N"); char reply = response.charAt(0); if(reply == 'Y'|| reply == 'y'){ JOptionPane.showMessageDialog( null,"That is cool!" ); } else{ JOptionPane.showMessageDialog( null,"I recommend you join now" ); } } }
In the above program, we are asking the user a question whose answer is either yes (Y) or no (N). We are then using the charAt() method to check the character that the user has entered which should be at position 0.
char reply = response.charAt(0);
The IF … ELSE statement is used to display the appropriate message depending on the character entered. Notice that we have taken care of the situation where the user may enter a lowercase y instead of uppercase Y by including the OR (||) comparison operator in the IF … ELSE statement.
if(reply == 'Y'|| reply == 'y'){
JOptionPane.showMessageDialog( null,"That is cool!" );
}
else{
JOptionPane.showMessageDialog( null,"I recommend you join now" );
}
I would recommend that you practice more on these two methods to have grip on their use. In the next lesson, we shall look at the Java replace() method.
<< Lesson 22 | Lesson 24 >>
Other related hubs...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 4
In Lesson 3, we learnt how to write a Java program in the Java code window and how to Run a Java program. In this lesson, we shall learn how to work with variables in Java and how to manipulate computer memory using variables. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window. - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ... - Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 5
One of the useful classes that handle inputs from a user in Java is the Scanner class. The Scanner class is located in the util (utility) package of the Java library.