Pages

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, January 26, 2020

Java: Count the number of blank characters in a given string

The following code is going to print a message with the number of white spaces in a given sentence.



First, we need to write the name of the procedure: countWhiteSpaces


public static void countWhiteSpaces() {

}


The above procedure is static because only one copy of the object is needed.

Then, we need Scanner, in order for the user to be able to write the sentence on the screen. We will also print indications for the user.

Scanner kb = new Scanner(System.in);
System.out.println("Enter a sentence...");

The kb variable will hold the sentence the user is going to type. We store this information into the String called sentence.

String sentence = kb.nextLine();

We need to count the figure out the length of the sentence, for that we user the .lenght() property.

int senLen = sentence.length();

Also, a variable needs to be initialized for counting the number of white spaces.

int blankSpaces = 0;

In order to check and count the number of white spaces in a sentence we will use a for loop. This is going to start in 0 and will iterate until the length of the sentence. The if statement is going to check whether each character is an empty space or not, and then increase the blankSpace variable.

for (int i = 0; i < senLen; i++) {
     if (sentence.charAt(i) == ' ') {
            blankSpaces++;
     }
}


Finally, we are going to print the result

System.out.println("The number of white spaces in your sentence is: " + blankSpaces);




The final code is going to look like this:


import java.util.Scanner;

public class couSpaces {

public static void main(String[] args) {
// TODO Auto-generated method stub
countWhiteSpaces();
}

public static void countWhiteSpaces() {

Scanner kb = new Scanner(System.in);
System.out.println("Enter a sentence...");

String sentence = kb.nextLine();
int senLen = sentence.length();
int blankSpaces = 0;

for (int i = 0; i <  senLen; i++) {

if (sentence.charAt(i) == ' ') {
blankSpaces++;
}

}

System.out.println("The number of white spaces in your sentence is: " + blankSpaces);
}

}

Thursday, August 18, 2016

Java: sum of the first N positive odd integer numbers

Write a fragment of code that will compute the sum of the first n positive
odd integers. For example, if n is 5, you should compute 1 +3 + 5 +7 + 9.

For doing so write the following code:


public static void sumNPositiveOddIntegers() {
 System.out.println("Escriba un numero entero...");
 Scanner kb = new Scanner(System.in);
 int n = kb.nextInt();

 int x = 1;
 int sum = 0;
 for (int i = 0; i < n; i++)
 {
  sum += x;
  x += 2;
  System.out.println(x);
 }
 System.out.println("The sum of first " + n + " positive odd integers is: " + sum);

}


Code explanation:

public static void sumNPositiveOddIntegers(): As we are printing the result we use void.

Scanner kb = new Scanner(System.in): Here we declare the variable kb with the method Scanner in order to read from the keyboard.

int n = kb.nextInt(): Assign to the variable n the integer value from the keyboard.

 int x = 1, sum = 0: Initialize the variables x and sum.

 for(int i = 0; i < n; i++): This loop initializes the variable I with value 0. Then it states: while i is less than n, then the i's variable value will be increasing.

sum += x: the variable sum will be storing the value of x during the loop.

x += 2: x will be increasing in pairs in order to sort the odd numbers. That's way we initialize it with value 1.

System.out.println(x): Prints out the x's value that will be the positive odd integer numbers.

Finally, the last line of code will print the sum of the previous numbers.







Saturday, June 18, 2016

Java: Calculate if a number is divisible by another number

A number x is divisible by y if the remainder after the division is zero. Write
a program that tests whether one number is divisible by another number. Reads both numbers from the keyboard.

public static void divisible() {

            Scanner kb = new Scanner(System.in);
            System.out.println("Enter the value of x...");
            int x = kb.nextInt();
            System.out.println("Enter the value of y...");
            int y = kb.nextInt();

            if (x % y == 0) {

                  System.out.println(x + " is divisible by " + y);

            } else {

                  System.out.println(x + " is not divisible by " + y);
            }


      }


Code explanation:

public static void divisible(): Here the void we use void method because it won't return, but print a result. 
Scanner kb = new Scanner(System.in): In this program we're going to read two integers from the keyboard. Scanner is used with this objetive. Note: In order to utilize Scanner we need to import the Scanner library. We must include the following line of code:  import java.util.Scanner.

System.out.println("Entrar un numero x..."): System.out.println prints a message though the console. In this case to give instructions to the user. 

int x = kb.nextInt(): Here we declare the variable x as an int, which is equals to the variable kb that we declared in Scanner.  nextInt() means it is going to take the next Integer value the user enter through the keyboard and is going to be stored at the variable x.

Then we repeat the code for variable y.

if (x % y == 0): In order to get the remainder from the division between two numbers we use the Mod method. In Java it is represented by the percentage sign (%).

The rest of the code prints if either x is divisible by y or no.