Site hosted by Angelfire.com: Build your free website today!

Simple Input / Output 

One of the major drawback of java lies in its Input system. Unlike the input command of qbasic and cin of c++ javas’ way of inputs is more error prone than its output due to wrong input, a program may crash down causing exception, which represents run time error. In Java the exceptions casued by input/output codes are best handled by appending the clasue “throws IOExceptions “ to the declaration of the main() method.

 

During input data flows through five objects :

1)       System.in

2)       Reader

3)       Input,

4)       A data object such as Name or age etc

5)       System.out

 

While reader, input and data objects are to be defined in the program , System .in and system.out objects are drawn from the system class

The flow of data as input may be perceived by the following flow diagram

 

Keyboard                               System.in               reader                     input

 

                                             Screen                    System.out             Data object

 

Input flow diagram

 

The three classes which are involved in the input process are IOE xception, Input Stream Reader and Buffered Reader

               Use import java.io *

               The object reader which is an instance of the InputStreamReader  class  act as a conduit which conveys data from keyboard to the input stream Syustem.in for onward passage into the program.

 

The object input which is tied up to the reader object is defined as an instance of the BufferReader class. This enables the object input to extract input in a convenient way. Using its readline() method, it can read an entire line of characters entered through the keyboard and deliver as a string object.

 

String employeeName = input.readLine();

 

The println() method is used to copy a string on the computer screen by the statement

 

 

InputStreamReader reader = new InputStreamreader(System.in);

Buffered Reader input  = new BufferedReader(reader);

 


 

Library Functions

 

Mathematical Functions : Math.abs() This function helps in finding the absolute value for any number. Eg. Math.abs(-12.25) = 12.25.

 

 Math.sqrt(n)  - returns the square root of the parameter n.

                              Eg. - Math.sqrt(25) = 5

 

Math.max(a,b) – This function  finds max of two number i.e. a,b.

                              Eg.          Math.Max(5,6) = 6

 

Math.min(a,b) – This function finds min of two number i.e a,b.

                                             Eg. Math.Min(6,7) = 6

 

Math.round(n) – The round function returns the integer closest to the argument.

                                             Math.round(4.7)  = 5

                                             Math.round(4.4) = 4

                                             Math.round(-4.2) = -4

 

Math.floor(n) – The floor function returns the smallest real number which is the equivalent of an integer but not greater  eg.  Math.floor(12.85) = 12

                                                                          

 

Math.ceil(n)  - The floor function returns the smallest real number which is the higher equivalent of an integer but not greater  eg. Math.ceil(12.85) = 12

 

 

Math.pow(p,r)  - assigns y the value of p raised to the power of r.

               y =  Math.pow(p,r)

 

 


 

String Class

String  class is again one of the commonly used class. The functions here are innumerable ie they cannot be changed . The operations are performed on the character strings. A string can be created Implicity. i.e either by  declaring  within quotes  like “classes are easy” or by using the concatenation  + “operation on two or more strings.

 

Various operations can be performed on strings. Comparing two strings are not done in the same way as primitive data types. For primitive data types  ‘= = ‘ operator can compare two data but for strings, the

 

   equals()  method is used for comparing.

 

               Str1 = “Java”;

               Str2 = “Java”;

               Str1 == str2 gives false

While Str1.equals(str2) gives true

 

Unlike other programming languages, Java strings are compared numerically by their Unicode values. Other languages use the ASCII code structures while Java uses the Unicode code structure. Advantage of using Unicode characters is that a huge number of characters are available  in it. While comparing  a true value is returned only if both string  objects are of the same length and have the same sequence of Unicode characters. It should be noted that there is difference between ‘R’ and ‘r’. Lower and upper case letters can be ignored using the equalsIgnoreCase() . This function  ignores the case of the letters and compares the two strings.

 

Str1 = “Cathedral”

Str2 = “Cathedral”

Str3 = “cathedral”

 

Str1.equals(Str2), gives true

 

Str1.equals(Str3), gives false

Str2.equalsIgnoreCase(Str3) gives true

 

There is another way of comparing , that is using the compareTo function. Here the return value is a

 

Str1.compareTo(str2) : Return < 0 when the first string is numerically less than the second string.

Str1.compareTo(str2) : Return  = 0 when the first string equals the second string equals the second string.

Str1.compareTo(Str2) Return >0 when the first string is numerically greater than the second string.

 

 charAt()  :  To find the character at a specified position of a string the charAt() function is used. It should be clear the index number of the first character in a string is 0.

 

String  Str1 = “Pride comes before a fall”

 

Str1.charAt(1) = ‘r’

Str1.charAt(4) =  ‘e’

 

indexOf() – if the position number of any character is desired , the indexOf() function is used. This method returns the index (an integer) of first occurrence of the character from the string.

 

String  Str1 = “Pride comes before a fall”

 

  Str1.indexOf(‘e’)  = 4

 

Though the character ‘e’ occurs at 4th , 9th , 13th & 17th index the value of the first index is returned here. Incase the characters is not found within the string a value of -1 is returned. To find the index of the characters in its last occurrence the lastindexOf() method is used.

 

Str1.lastIndexOf(‘e’) = 17

 

In case the position of  a character is required after a certain index number, then indexOf(char,int) might be used. The second parameter specifies the index after which the search for the first occurrence of the character is going to start.

 

Str1.indexOf(‘e’,7) = 9

 

Like characters, even index or position of string can also be found out.  The first occurrence of the substring  is found from the given string.

 

Str1.indexOf(“ide”) = 2

Str1.indexOf(“all”) = 22

 

Similarly the index of the substring can be found out starting from any particular can be found out starting from any particular index also.

Str1.indexOf(‘ome’,4) = 7

 

Incase the substring  is not found with the given string a value -1 is returned.

 

toUpperCase() function is used to change to upper case.

 

toLowerCase() function is used to change to lower case

 

replace(char,char) is used to replace a character in a string   is used. The function is replaces the first parameter, which is a character with the second parameter, which is character.

 

String  str = “netaji Subhas”

 

 str.replace(‘n’,’N’)

Would return str = “Netaji Subhas”. This function converts the string by replacing  all occurrences of the old character with the new character.

 

The replace function replaces the characters or string temporarily. The change is not permanent and does not affect the orginal string.

 

 


 

Substring()

To extract a substring from a given string the substring() function is utilized. The substring extracts the characters from the start index to the end index. Both these parameters have to be specified.

 

String str = “University in Diversity”.

 

Str.substring(0,3) = “Unit”

Str.substring(9,12) = “Dive”

 

The first integer defines the starting index from where the first character of the substring is to start. The second integer specifies the last index indicating the end of substring.

 

length() – this function finds the length of the string;

 

String name1 = “Harish”;

 

name1.length();

 

concat() – Concatenates the specified string to the end of this string

         concatenation operator ie + achieves same as concat method.

              

               String Name1 = “Harish”

               String Name2 = “Gupta”

              

                  Name1.concat(Name2)

 


 

Programs on Library Functions

 

1.       WAP to find the length of a string?

 

       class ex1

    {

        public void print(String str)

        {

            int L = 0;

            L = str.length();

            System.out.println("The length of the string is " +L);

        }

    }

 

2.       WAP to display the number of words in a given text.

class numwords

    {

        public void compute(String str)

        {

            int i,j,k = 0, w = 1;

            char sl;

            k = str.length();

            for(i =0; i< k;i++)

            {

                sl.str.charAt(i);

                if (sl == ' ')

                {

                    w  = w+ 1;

                }

            }

            System.out.println("The number of words in the string is  " + w);

        }

    }

3.       WAP to display the number of vowels in a given string or (accept it from keyboard)

 class ex2

    {

        public void compute(String s)

        {

            int i,j,k = 0 , v = 0;

            char s1 ;

            k  = s.length();

            for(i = 0; i< k ; i++)

            {

                if (s1 = = ‘a’ || s1 = = ‘e’ || s1 = = ‘i’ || s1 = = ‘o’ || s1 = = ‘u’ )

                {

                    w = w+1;

                }

            }

            System.out.println(“The number of vowels in a string is  “ + w);  

        }

    }

4.       WAP to display the string in reverse

               class reverse 

    {

        public void compute(String s)

        {

            int i,j,k = 0;

            String ss = " ";

            char s1;

            k = s.length();

            for(i = k-1; i>= 0 ;i--)   

            {

                s1 = s.charAt(i);

                ss  = ss+s1;

            }

            System.out.print("The string is " +s);

            System.out.println("The string in reverse is  "+ ss);

        }

    }         

5.       WAP to check if the entered string is a palindrome or not.

 

   class palid

    {

        public void comp(String s)

        {

            int i,j,k =0;

            String ss =  " ";

            char s1;

            k = s.length();

            for(i = k-1; i>= 0; i--)

            {

                s1 = s.charAt(i);

                ss = ss+1;

            }

            System.out.print("The string is " +s);

            System.out.println("The string in reverse is  "+ ss);

            if(s.equals(ss))

            System.out.print("The string is a palindrome ");

            System.out.println("The string is not a palindrome");

        }

    }

 

 

 

 Home