Search Logic Blocks

Tuesday, July 14, 2020

Java: Let's Practice - Count the words

Let's write a program to let user write a sentence and count the words in the entered sentence - 

I have already explained in my earlier post - How to get input characters and strings from console, how to let the user enter the characters using the keyboard through Scanner class and nextLine() method. We know String is a class and it has methods to manipulate the strings and characters, we will see new method split(). You can get all the information about String class through API documentation.

Create a logic 
  1. We need to prompt the user to enter the sentence. The program reads the sentence using method  nextLine() of Scanner class. I have already explained how to get any type of text from the user using the class Scanner in post Get input from console. As soon as the user presses the enter button, the nextLine() reads the whole text entered by the user and returns the String object.
  2. Now, the program needs to separate words. A sentence has words separated by space " ". String class has a method split(), which creates an array of strings by separating text with given separator passed as a parameter to the method.
  3. By using space " " as a separator, the split() method returns array of all the words entered by the user. We have discussed arrays already in the post Arrays. The length of an array will tell us how many words user has entered.
Program to find the count of words in a sentence entered by user
import java.io.IOException;
import java.util.Scanner;

public class Sentence {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence:");
String str = sc.nextLine();
String words[] = str.split(" ");
int wordCount = words.length;
System.out.println("Word Count is " + wordCount);
}
}
Let's analyze above program using the logic steps above -
String str = sc.nextLine();
The program reads the sentence entered by the user using method nextLine() and the returned string is assigned to the String variable str.
String words[] = str.split(" ");
String str is converted to a String array words[] using the split method and separator as space " ".
int wordCount = words.length;
Each array has a property length which returns the number of elements in the array. In this case, words.length; returns the number of words in the sentence.
if(prime) System.out.print(i + " ");
Output will be like -

Enter the sentence:
Do more of what makes you Happy!!
Word Count is 7

Note: Here in the above program, we used Scanner class and String class methods. These two classes are important classes and will be used more later. You can find all the detailed documentation regarding Scanner classString class and Array in the links.

Saturday, July 11, 2020

Java: Scope of variables

How the variables can be accessed by the rest of the program, depends on where the variable is declared and which access modifier is used. There are 2 kinds of variables - class variables and block variables. The area, where the variables can be accessed, is called as the scope of variables.

Class Variables -
Class variables are the variables declared with the class definition and are not part of any method. There are 3 access modifiers for class variables - public, private and protected. We have already discussed these access modifiers in previous posts - Encapsulation and Inheritance. Let's review the definition of three access modifiers -

Public - is the default access modifier. The variables declared as public can be accessed by methods of any other class outside. public word is used to declare public variables. If there is no access modifier given at the time of variable declaration, by default it is public.
public int pubvar; // Public Class Variable
Private - The variables are declared as private and can be accessed by only the class itself. These variables can not be accessed outside from the class. Even the child classes / sub classes of the class can not access the private variables.
private int privar; // Private Class Variable
Protected - These variables are declared as protected and can be accessed by all the classes in the same package, but only its sub classes in the different package. But other classes in different package, which are not the sub classes, can not access the protected variables.
protected int provar; // Protected Class Variable
Block Variables -
Two enclosing parentheses - opening ({) and closing (}) create a block. Any variables declared between these two parentheses are called block variables and their scope is block itself. The block variables can not be accessed outside from the block. We already know about method as one block -
public void test() {
int i = 0;
}


// i = 5; (Variable i is not accessible)
Another example is for loop -
public void test() {
for(int i = 0; i < 5; i++) {

System.
out.println(i);
}

// System.out.println(i); (Variable i is not accessible)
}
Blocks can be defined with only parentheses also, so the variable can not be accessed outside of the parentheses -
public void test() {
{

int i = 5;
}

// System.out.println(i); (Variable i is not accessible)
}