0% found this document useful (0 votes)
4 views9 pages

Java Exception Handling and String Programs

The document outlines a practical experiment for a Java programming course, focusing on reading and writing objects to files. It includes various coding exercises such as reversing a string, counting vowels, and separating characters in a string. The document also emphasizes the importance of exception handling in maintaining the normal flow of applications.

Uploaded by

AYUSH SHARMA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Java Exception Handling and String Programs

The document outlines a practical experiment for a Java programming course, focusing on reading and writing objects to files. It includes various coding exercises such as reversing a string, counting vowels, and separating characters in a string. The document also emphasizes the importance of exception handling in maintaining the normal flow of applications.

Uploaded by

AYUSH SHARMA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

University Institute of Engineering

Department of Electronics & Communication Engineering

Experiment:- 8
Student Name:- Mohit UID:-21BEC1136
Branch: Electronics and Communication Section/Group:-1(B)
Semester:-4th Date of Performance:-23/04/2023
Subject Name:-Java programming
Subject Code:-21ECP-227

1. Aim of the practical:-


Write a program to read and write an object to file.
2. Tool Used:- Java online compiler

3. Basic Concept/ Command Description:-


Exception Handling in Java is one of the
powerful mechanisms to handle runtime errors so that the normal flow of the application can
be maintained. Exception Handling is a mechanism to handle runtime errors such as Class
Not Found Exceptions, IO Exception, SQL Exception, Remote Exception, etc. The core
advantage of exception handling is maintaining the application's normal flow. An exception
normally disrupts the normal flow of the application; that is why we need to handle
exceptions.

4. Code:-
P1: Write a Java program to reverse a string without using any string API.

import java. util.*;


class ReverseString
{ public static void
main(String args[])
{ String original,
reverse = "";
Scanner in =
University Institute of Engineering
Department of Electronics & Communication Engineering

new
Scanner([Link]
[Link]
t("Enter a string to
reverse: ");
original =
[Link]()
int length =
[Link]()
for ( int i =
length - 1 ; i >= 0 ; i--
)
reverse =
reverse +
[Link](i);

[Link]
tln("Reverse of entered
string is: "+reverse);
}
}

Output:
University Institute of Engineering
Department of Electronics & Communication Engineering

P2: Write a Java program to count the number of vowels in a given string and print
them.

import java.
util.*; class
CountVowels
{ public static void
main(String[] args)
{ int count = 0;
char[] vowels =
new char[] {'a', 'e', 'i',
'o', 'u'};
Scanner scan =
new
Scanner([Link]);
System.
[Link]("Enter a
string: ");
String str =
[Link]().to lower
Case();
for (int i = 0; i<
[Link](); i++ )
{
for (int j =
0; j < 5; j++)
{
if
([Link](i) ==
vowels[j coun
}
}
University Institute of Engineering
Department of Electronics & Communication Engineering

[Link]("
Total vowel present in

given string:
"+count);
}
else
System.
out. println("no
vowels present in
given string.");
}
}

Output:

P3: Write a Java program that accepts a string from the user and displays vowel
characters, digits, and blank spaces separately.

import [Link].*;
class Alphabets
{
University Institute of Engineering
Department of Electronics & Communication Engineering

public static void


main(String args[])
throws IOException
{
String str;

int vowels = 0,
digits = 0, blanks = 0;
char ch;
BufferedReader
br = new
BufferedReader(new
InputStreamReader(Sy
[Link]));
System.
[Link]("Enter a
String : ");
str =
[Link]();
for(int i = 0; i <
[Link](); i ++)
{
ch =
[Link](i);
if(ch == 'a'
|| ch == 'A' || ch ==
'e' || ch == 'E' || ch
== 'i' ||
University Institute of Engineering
Department of Electronics & Communication Engineering

ch == 'I' ||
ch == 'o' || ch == 'O'
|| ch == 'u' || ch ==
'U')
vowels
++;
else
if([Link](ch)
)
digits
++ else

If([Link]
a ce(ch)) blanks ++;
}
[Link]
tln("Vowels : " +
vowels);
[Link]
tln("Digits : " + digits);

[Link]
tln("Blanks : " +
blanks);
[Link]
tln("\n\n\n ");
}
}
University Institute of Engineering
Department of Electronics & Communication Engineering

Output:

P4: Write a program to reverse the words of the string object.

import java.
[Link]; class
ReverseWordofString
{

public static String


reverseString(String s)
{
String[] str1 =
[Link](" ");
String result1
= "";
for (int i =
[Link]-1; i >= 0;
i--)
{
result1
+= str1[i]+" ";
University Institute of Engineering
Department of Electronics & Communication Engineering

}
return result1;
}
public static void
main(String args[])
{
Scanner scan =
new
Scanner([Link]);
[Link]
ntln("Enter the String:
");
String str =
[Link]();
String result =
ReverseWordofString.r
everseString(str);
[Link]
ntln("\nGiven String:

"+str);
[Link]
ln("\nReverse String:
"+result);
}
}
Output:

You might also like