Java || Using If Statements, Char & String Variables
As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.
===== SINGLE CHAR =====
This example will demonstrate a simple program using char, which checks to see if you entered the correctly predefined letter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: GuessALetter.java // Description: Demonstrates using char variables // ============================================================================ import java.util.Scanner; public class GuessALetter { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please try to guess the letter I am thinking of: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Use an If Statement to check equality if (userInput == 'a' || userInput == 'A') { System.out.println("You have Guessed correctly!"); } else { System.out.println("Sorry, that was not the correct letter I was thinking of"); } }// end main }// http://programmingnotes.org/ |
Notice in line 19 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 26 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 26 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a')
as your if statement, and notice the difference.
The resulting code should give this as output
Please try to guess the letter I am thinking of: A
You have Guessed correctly!
===== CHECK IF LETTER IS UPPER CASE =====
This example is similar to the previous one, and will check if a letter is uppercase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: CheckIfUppercase.java // Description: Demonstrates checking if a char variable is uppercase // ============================================================================ import java.util.Scanner; public class CheckIfUppercase { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please enter an UPPERCASE letter: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Checks to see if inputted data falls between uppercase values if ((userInput >= 'A') && (userInput <= 'Z')) { System.out.println(userInput + " is an uppercase letter"); } else { System.out.println(userInput + " is not an uppercase letter"); } }// end main }// http://programmingnotes.org/ |
Notice in line 26, an If statement was used, which checked to see if the user inputted data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)
IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')
THEN it is an uppercase letter
The resulting code should give this as output
Please enter an UPPERCASE letter: p
p is not an uppercase letter
===== CHECK IF LETTER IS A VOWEL =====
This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: CheckIfVowel.java // Description: Demonstrates checking if a char variable is a vowel // ============================================================================ import java.util.Scanner; public class CheckIfVowel { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables char userInput = ' '; System.out.print("Please enter a vowel: "); // get a single character from the user data input userInput = cin.next().charAt(0); // Checks to see if entered data is A,E,I,O,U,Y if ((userInput == 'a')||(userInput == 'A')||(userInput == 'e')|| (userInput == 'E')||(userInput == 'i')||(userInput == 'I')|| (userInput == 'o')||(userInput == 'O')||(userInput == 'u')|| (userInput == 'U')||(userInput == 'y')||(userInput == 'Y')) { System.out.println("Correct, " + userInput + " is a vowel!"); } else { System.out.println("Sorry, " + userInput + " is not a vowel"); } }// end main }// http://programmingnotes.org/ |
This program should be very straight forward, and its basically checking to see if the user entered data is the letter A, E, I, O, U or Y.
The resulting code should give the following output
Please enter a vowel: o
Correct, o is a vowel!
===== HELLO WORLD v2 =====
This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Mar 30, 2021 // Taken From: http://programmingnotes.org/ // File: HelloWorldString.java // Description: Demonstrates using string variables // ============================================================================ import java.util.Scanner; public class HelloWorldString { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables String userInput = ""; System.out.print("Please enter a sentence: "); // get a string from the user userInput = cin.nextLine(); // display message to user System.out.println("You entered: " + userInput); }// end main }// http://programmingnotes.org/ |
The resulting code should give following output
Please enter a sentence: Hello World!
You entered: Hello World!
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac YOUR_CLASS_NAME.java
*** To run the compiled program, simply type this command:
java YOUR_CLASS_NAME
thank you for uploading this