Find the highest and the lowest character in a word through java
09-09-2018    229 times
highest lowest character java

Data members/instance variables:

str : to store a word

max : to store the highest alphabetical character present in the word

min : to store the lowest alphabetical character present in theword

Member functions:

WordOp() : default constructor

Void input() : to accept the word in UPPERCASE

Void calcMaxMin() : to find the highest and the lowest character present in the word. Void display() : to display the word along with the highest and the lowestcharacter

Example: If word is "TWEAK" then

Highest Character =W

Lowest Character = A

Read a word using next() method of Scanner class. Now extract the individual character from the word using charAt() method and store into a charcter variable called ch. Now compare the ch to max or min and store the highest character in max variable and lowest character in min variable. Now print the variable.

Tutorial at home
import java.util.*;

public class WordOp
{
    private String str;
    private char max,min;
    public WordOp()
    {
        max='A';
        min='Z';
    }
    void input()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a String in a upper case : ");
        str=sc.next().toUpperCase();
    }
    void calcMaxMin()
    {
        char ch;
        for(int i=0;i<str.length();i++)
        {
            ch=str.charAt(i);
            if(ch<min)
                min=ch;
            if(ch>max)
                max=ch;
        }
    }
    void display()
    {
        System.out.println("Highest Character = " + max); 
        System.out.println("Lowest Character = " + min);
    }
    public static void main(String []args)
    {
        WordOp op=new WordOp();
        op.input();
        op.calcMaxMin();
        op.display();
    }
}

Tutorial at home

Enter a String in a upper case :
TWEAK
Highest Character = W
Lowest Character = A

softetechnologies
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post