String Comparision JAVA by Ravinder Nath Rajotiya - April 13, 2021April 14, 20210 Comparing Strings String is created from String class. let us declare two string variables as name1 and name2. String name1=”Peter” String name2=”Peter”; Ravinder Rajotiya Name1 name2 Steps in string object creation in JAVA Step-1 Step-2 First the string object with name "Ravinder" createrd in memory Next the string variable name1 will be created in memory which will hold the address of the text "Ravinder" and not the text itself. Similarily steps for name2 variable creation is: Step-1 Step-2 First the string object with name "Rajotiya" createrd in memory Next the string variable name2 will be created in memory which will hold the address of the text "Rajotiya" and not the text itself. Comparing Strings: We don't compare strings using relational operators. The simple reason is that the relational operators on strings
if, if-else, if-else-if, nested if JAVA by Ravinder Nath Rajotiya - April 13, 20210 if, if-else, if-else-if, nested if The if statements are the decision structures used in JAVA. It is most useful decision structure for testing for a condition and branch to the relevant statement in the program. Simple and Poorly written program An efficient programming approach package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { int number; String input; input= JOptionPane.showInputDialog("Please enter a number"); number = Integer.parseInt(input); if(number == 5) { JOptionPane.showMessageDialog(null, "The number is exactly equal to 5"); } if(number >5) { JOptionPane.showMessageDialog(null, "The number is > 5"); } if(number > 10) { JOptionPane.showMessageDialog(null, "The number is > 10"); } if(number <>>5) { JOptionPane.showMessageDialog(null, "The number is < 5"); } } } package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { int number; Scanner in =new Scanner(System.in); System.out.println("Enter a number"); number=in.nextInt(); if(number ==5) { JOptionPane.showMessageDialog(null, "The number is exactly equal
Operators in JAVA JAVA by Ravinder Nath Rajotiya - April 13, 2021April 13, 20210 Decision Structure Deceision structure are used so that the program doesn’t have to do the same thing each time a program is executed. They basically answer a question on the spot during execution. Decesion structure need a question and this is done through Boolean. As we have seen boolean are primitive data types. It represent two values true or false. They can be set directly but is usually by using a set of operators including but not limited to relational and logical operators. Operator Meaning > Is greater than < Is less than >= Greater than or equal to <= Less than or equal to == Is equal to != Is not equal to We can use these operatots in Boolean expressions. Table belows gives some common ways of using the relational operators : Operator Meaning X > Y Is X
Getting input from user using JoptionPane class JAVA by Ravinder Nath Rajotiya - April 12, 2021April 12, 20210 Getting input from userusing JoptionPane class In this class we are going to larn JoptionPane class to get the interactive inputs from the keyboard or files.. JoptionPane class is defined in java.swing*.* package. As soon as we typeJOptionPane() the system prompts for a hint to java.swing*.* package we can ALT+Enter to include that package. JoptionPane.showInputDialog(“”);the showInputDialog() method in JoptionPane class accepts string as argument. See the example below: JOptionPane.showInputDialog(“Please enter your name”); This method returns only strings and not numbers. To input integers or float you need to input as string and then convert it to int (see example-2). it returns string, so should be saved in string variable. Now, let us see how we can display these values. We will now use JOptionPane.showmessageBox(); JOptionPane.showMessageBox(null,
Reading Input from Keyboard-using Scanner Class JAVA by Ravinder Nath Rajotiya - April 12, 2021April 12, 20210 Reading input from user A program without any interactin is of no use. We must have some way of talking and asking the program to provide some sort of inputs. Scanner class is used to get the input from a keyboard, from a file etc. Here is an example: What you need to do: a. declare variable to hold values from input(keyboard) b. declare Scanner class object using Scanner kb = new Scanner(System.in);Here kb is an object of Scanner class that makes connection with the keyboard by the scanner class and in the new Scanner() pass System.in method to read from the input device c. Repeat the above step-b for more variables d. declare a variable double to hold the average of the two numbers. Here is