Java HelpDesk

Core Java Assignments
Strings

Q1. Enter a string from the user and count the length of the entered string.

//Enter a string from the user and count the length of the entered string.

import java.util.Scanner;
public class String_Length {
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
String str;
int n;
System.out.println("Enter a string");
str=sc.nextLine();
n=str.length();
System.out.println("The length of the string is "+n);

}//end of main method
}//end of class String_Length

Q2. Enter a string “COMPUHELP” from the user and count the number of vowels in it.

// Enter a string “COMPUHELP” from the user and count the number of vowels in it.

import java.util.Scanner;

public class Count_Vowels {
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
String str;

int count=0;
int n;

System.out.println("Enter a string");
str=sc.nextLine();
n=str.length(); //counting the length of the string.

for(int i=0;i<n;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{

count++;

}

}
System.out.println("Number of vowels present in string are "+count);

}//end of main method
}//end of class Count_Vowels

Q3. Enter a string from the user in lower case and change all the vowels of the entered string in upper case.

/*Enter a string from the user in lower case and change all the vowels of the entered
string in upper case.*/

import java.util.Scanner;

public class V_UpperCase {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str,str1="";
int n,i;
System.out.println("Enter a string in Lowercase");
str=sc.nextLine();
n=str.length();

for(i=0;i<n;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{

str1=str1+str.substring(i,i+1).toUpperCase();

}//end of if block
else
{
str1=str1+str.substring(i,i+1);

} //end of else block

}//end of for loop
System.out.println(str1);

}//end of main method
}//end of class V_UpperCase

Q4. Enter a string from the user and count the number of words present in the entered string.

//Enter a string from the user and count the number of words present in the entered string.
import java.util.Scanner;

public class Count_Words {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int n, i,space=0;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();

for(i=0;i<n;i++)
{
if(str.charAt(i)==' ')
{
space++;
}

}// end of for loop

System.out.println("The number of words present in the string are "+(space+1));

} //end of main method
}//end of class Count_Words

Q5. Enter a String from the user and print those characters that they have space before them.

//Enter a String from the user and print those characters that they have space before them.

import java.util.Scanner;

public class Space_Before_Character {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String str;
int n, i;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();

for(i=0;i<n;i++)
{
if(str.charAt(i)==' ')
{
System.out.println(str.charAt(i+1));

}

}//end of for loop

}//end of main method
}//end of class Space_Before_Character

Q6. Enter a string from the user and count how many times only two vowels occur together in the entered string.

/*Enter a string from the user and count how many times only two vowels occur
together in the entered string.*/

import java.util.Scanner;

public class Two_Vowels_Together {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String str;
int n, i,count=0;
System.out.println("Enter a string");
str = sc.nextLine();
n = str.length();

for(i=0;i<n-1;i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
if(str.charAt(i+1)=='a'||str.charAt(i+1)=='e'||str.charAt(i+1)=='i'||str.charAt(i+1)=='o'||str.charAt(i+1)=='u')
{

count++;
} //end of inner if

}//end of outer if
}//enf of for loop
System.out.println("The number of vowels together are "+count);
}//end of main method
}//end of class Two_Vowels_Together

Q7. Enter a string from the user and print the first and last word of the entered string.

//Enter a string from the user and print the first and last word of the entered string.

import java.util.Scanner;

public class First_Last_Word {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
int wl=0;
System.out.println("Enter a string");
str = sc.nextLine();
String str1[]=str.split(" ");
wl=str1.length;

System.out.println(str1[0]);
System.out.println(str1[wl-1]);
}//end of main method
}//end of class First_Last_Word

Q8. Enter a string from the user in uppercase and change each vowel present at the end of the words of the entered string in lowercase.

/*Enter a string from the user in uppercase and change each vowel present at the end of
the words of the entered string in lowercase.*/

import java.util.Scanner;

public class Vowel_End_UpperCase {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str,strcat="";

int n,wl;

System.out.println("Enter a string in Uppercase");
str=sc.nextLine();
String str1[]=str.split(" ");
wl=str1.length;

for(int i=0;i<wl;i++)
{
n=str1[i].length();

if(str1[i].charAt(n-1)=='A'||str1[i].charAt(n-1)=='E'||str1[i].charAt(n-1)=='I'||str1[i].charAt(n-1)=='O'||str1[i].charAt(n-1)=='U')
{
strcat=strcat+str1[i].substring(0,n-1)+str1[i].substring(n-1).toLowerCase()+" ";
}
else
{
strcat=strcat+str1[i]+" ";
}

}
System.out.println(strcat);

}//end of main method
}//end of class Vowel_End_UpperCase

Q9. Enter two strings from the user and check if both the strings are equal or not. If the two strings are equal then print “strings are equal” otherwise print “strings are not equal”.

/*Enter two strings from the user and check if both the strings are equal or not. If the two
strings are equal then print “strings are equal” otherwise print “strings are not equal”. */

import java.util.Scanner;

public class Two_Strings_Equal {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str1,str2;
System.out.println("Enter first string");
str1=sc.nextLine();
System.out.println("Enter second string");
str2=sc.nextLine();

if(str1.equalsIgnoreCase(str2))
{
System.out.println("Strings are equal");
}
else
{
System.out.println("Strings are not equal");
}
}// end of main method
}//end of class Two_Strings_Equal

Q10. Enter a string from the user and concatenate the alternative words of the string together.

//Enter a string from the user and concatenate the alternative words of the string together.

import java.util.Scanner;

public class Concat_Alternative_Words {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
System.out.println("Enter a string");
str=sc.nextLine();
int n,wl;
n=str.length();

String str1[]=str.split(" ");
wl=str1.length;

for(int i=0;i<wl;i++)
{
if(i%2==0)
{
System.out.print(str1[i]+" ");
}

}

}//end of main method
}//end of class Concat_Alternative_Words

Q11. Enter a string from the user in lowercase and capitalize each word of the string.

import java.util.Scanner;

//Enter a string from the user in lowercase and capitalize each word of the string.
public class Capitalize_Each_Word {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String str,strcat="";
System.out.println("Enter a string in lowercase");
str=sc.nextLine();

String str1[]=str.split(" ");
int wl,n;
wl=str1.length;

for(int i=0;i<wl;i++)
{
n=str1[i].length();
strcat=strcat+str1[i].substring(0,1).toUpperCase()+str1[i].substring(1,n)+" ";
}
System.out.println(strcat);
}//end of main method
}//end of class Capitalize_Each_Word

Q12. Enter a string “Compuhelp” from the user and print only “help” from the entered string.

//Enter a string “Compuhelp” from the user and print only “help” from the entered string.

import java.util.Scanner;

public class Print_Help {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str,strcat="";
System.out.println("Enter a string");
str=sc.nextLine();
int n=str.length();
System.out.println(str.substring(5,n));

}//end of main method
}//end of class Print_Help

Q13. Enter a string from the user and find the first and last index number of the character entered by the user.

/*Enter a string from the user and find the first and last index number of the character
entered by the user.*/

import java.util.Scanner;

public class Index_First_Last {
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
String str;
char ch;
System.out.println("Enter A string");
str=sc.nextLine();
System.out.println("Enter a character");
ch=sc.next().charAt(0);
System.out.println("The first index number of the entered character is "+str.indexOf(ch));
System.out.println("The last index number of the entered character is "+str.lastIndexOf(ch));


}//end of main method
}//end of class Index_First_Last

Q14. Enter a string “Compuhelp” from the user and replace the string “help” with “ter” so that the entered string become “Computer”.

Singleton class control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes.

Q. What do you mean by Constructor?

/*Enter a string “Compuhelp” from the user and replace the string “help” with “ter”
so that the entered string become “Computer”.*/

import java.util.Scanner;

public class Replace_String {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str1,str2;
System.out.println("Enter a string compuhelp");
str1=sc.nextLine();
str2=str1.replace("help","ter");
System.out.println(str2);
}//end of main method
}//end of class Replace_String


Swings in Java.


Example of JFrame in Java.

import javax.swing.*;

public class Frame_Compuhelp {
public static void main(String[] args) {
JFrame f=new JFrame("Frame Compuhelp");

f.setSize(300,300);
f.setVisible(true);
}//end of main method
}//end of class Frame_Compuhelp

Example of JFrame in Java using extends JFrame.

import javax.swing.*;

class Use_Frame extends JFrame
{
Use_Frame()
{
setTitle("Frame Compuhelp");
setSize(300, 300);
setVisible(true);
}
}//end of class Use_Frame

public class Extends_JFrame_Compuhelp
{
public static void main(String[] args) {

new Use_Frame();
}//end of main method

}//end of class Extends_JFrame_Compuhelp

Example of JButton in Java.

import javax.swing.*;
public class Button_Compuhelp {
public static void main(String[] args) {
JFrame f=new JFrame("Button Compuhelp");
JButton b=new JButton("Click");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(200,200);
f.setLayout(null);
f.setVisible(true);
}//end of main method
}//end of class Button_Compuhelp

Example of JButton with ActionListener in Java.

//Changing the text of Button by clicking on that Button.
                 
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Using_Listener extends JFrame implements ActionListener
{
JButton btn;
Using_Listener()
{
btn=new JButton("click me");
btn.setBounds(50,100,110,30);
add(btn);
btn.addActionListener(this);
setTitle("JButton Compuhelp");
setSize(300,300);
setLayout(null);
setVisible(true);

}//end of constructor

public void actionPerformed(ActionEvent e)
{

btn.setText("Compuhelp");

}//end of method actionPerformed

}//end of class Using_Listener
public class JButton_Actionlistener_Compuhelp {
public static void main(String[] args) {
new Using_Listener();

}//end of main method
}//end of class JButton_Actionlistener_Compuhelp

Example of JLabel in Java.

import javax.swing.*;
public class Label_Compuhelp {
public static void main(String args[])
{
JFrame f= new JFrame("Label Compuhelp");
JLabel l1,l2;
l1=new JLabel("Label First");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Label Second");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);

}//end of main method
}//end of class Label_Compuhelp

Example of JLabel with ActionListener in Java.

// Displaying message on JLabel by clicking on Button
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Using_JLabel_AL extends JFrame implements ActionListener
{
JButton btn;
JLabel lbl;
Using_JLabel_AL()
{
btn=new JButton("Get Message");
lbl=new JLabel();
lbl.setBounds(50,100, 250,20);
btn.setBounds(50,150,110,30);
btn.addActionListener(this);
add(btn);add(lbl);
setTitle("JLabel Compuhelp");
setSize(300,300);
setLayout(null);
setVisible(true);

} //end of constructor
public void actionPerformed(ActionEvent e)
{
lbl.setText("Welcome to Compuhelp");

}//end of method actionPerformed
}
public class JLabel_AL_Compuhelp {
public static void main(String[] args) {
new Using_JLabel_AL();
}//enf of main method
}//end of class JLabel_AL_Compuhelp

Example of JTextField in Java.

import javax.swing.*;
public class TextField_Compuhelp {
public static void main(String args[])
{
JFrame f= new JFrame("TextField Compuhelp");
JTextField t1,t2;
t1=new JTextField("Welcome to Compuhelp");
t1.setBounds(50,100, 200,30);
t2=new JTextField("Thanks to Visit Us");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,300);
f.setLayout(null);
f.setVisible(true);

}//end of main method

}//end of class TextField_Compuhelp


Example of JTextArea in Java.

import javax.swing.*;
public class TextArea_Compuhelp {

TextArea_Compuhelp()
{
JFrame f= new JFrame("TextArea Compuhelp");
JTextArea area=new JTextArea("Welcome to Compuhelp");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);

}//end of Constructor

public static void main(String args[])
{
new TextArea_Compuhelp();

}//end of main method
}//end of class TextArea_Compuhelp

Example of JPasswordField in Java.

import javax.swing.*;
class Using_PasswordField extends JFrame
{

Using_PasswordField()
{
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Enter Password:");
l1.setBounds(20,100, 100,30);
value.setBounds(120,100,100,30);
add(value); add(l1);
setTitle("Password_Compuhelp");
setSize(350,300);
setLayout(null);
setVisible(true);
}

}//end of class Using_PasswordField

public class PasswordField_Compuhelp
{
public static void main(String[] args) {

new Using_PasswordField();

} //end of main method

}//end of class PasswordField_Compuhelp

Example of JCheckBox in Java.

import javax.swing.*;

class Using_JCheckBox extends JFrame
{
Using_JCheckBox()
{
JCheckBox checkBox1 = new JCheckBox("Core Java",true);
checkBox1.setBounds(100,100, 100,50);
JCheckBox checkBox2 = new JCheckBox("Core Python");
checkBox2.setBounds(100,150, 100,50);
add(checkBox1);
add(checkBox2);
setTitle("JCheckbox Compuhelp");
setSize(350,300);
setLayout(null);
setVisible(true);

}

}//end of class Using_JCheckBox

public class JCheckBox_Compuhelp {
public static void main(String[] args) {
new Using_JCheckBox();

}//end of main method
}//end of class JCheckBox_Compuhelp

Example of JCheckBox with ItemListener in Java.

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

class Using_JCheckBox_AL extends JFrame implements ItemListener
{
JLabel lbl;
JCheckBox ch1,ch2;
String str="";
Using_JCheckBox_AL()
{
ch1=new JCheckBox("Core Java");
ch2=new JCheckBox("Core Python");
lbl=new JLabel();
lbl.setBounds(150,50,200,50);
ch1.setBounds(150,100, 100,50);
ch2.setBounds(150,150, 100,50);
add(lbl); add(ch1);add(ch2);
ch1.addItemListener(this);
ch2.addItemListener(this);
setTitle("JCheckBox Compuhelp");
setSize(400,300);
setLayout(null);
setVisible(true);

} //end of Constructor

public void itemStateChanged(ItemEvent e)
{


if (e.getSource() == ch1)
{
if (e.getStateChange() == 1)
str = str + "selected : " + ch1.getText();
lbl.setText(str);
}
else
{
if (e.getStateChange() == 1)
str=str+","+ch2.getText();
lbl.setText(str);
}

} //end of method itemStateChanged
} //end of class Using_JCheckBox_AL

public class JCheckBox_AL_Compuhelp {

public static void main(String[] args) {

new Using_JCheckBox_AL();

}//end of main method
}//end of class JCheckBox_AL_Compuhelp

Example of JRadioButton in Java.

import javax.swing.*;

class Using_JRadioButton extends JFrame
{
Using_JRadioButton()
{
JRadioButton r1=new JRadioButton("Male");
JRadioButton r2=new JRadioButton("Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
add(r1);add(r2);
setTitle("JRadioButton Compuhelp");
setSize(350,300);
setLayout(null);
setVisible(true);
}

}// end of class Using_JRadioButton

public class JRadioButton_Compuhelp {
public static void main(String[] args) {
new Using_JRadioButton();
}// end of main method
}// end of class JRadioButton_Compuhelp

Example of JRadioButton with ItemListener in Java.

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

class Using_JRadioButton_AL extends JFrame implements ItemListener
{
JRadioButton r1,r2;
String str="";
JLabel lbl;
Using_JRadioButton_AL()
{
r1=new JRadioButton("Male");
r2=new JRadioButton("Female");
lbl=new JLabel();
lbl.setBounds(75,150,150,50);
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
add(r1);add(r2);add(lbl);
r1.addItemListener(this);
r2.addItemListener(this);
setTitle("JRadioButton Compuhelp");
setSize(350,300);
setLayout(null);
setVisible(true);

}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==r1)
{

str="";
str=str+r1.getText();
lbl.setText("You selected : "+str);

}
else if(e.getSource()==r2)
{

str="";
str=str+r2.getText();
lbl.setText("You selected : "+str);

}

}//end of method itemStateChanged

}//end of class Using_JRadioButton_AL

public class JRadioButton_AL_Compuhelp {
public static void main(String[] args) {

new Using_JRadioButton_AL();

}// end of main method
}// end of class JRadioButton_AL_Compuhelp

Example of JComboBox in Java.

import javax.swing.*;

class Using_JComboBox extends JFrame
{
Using_JComboBox()
{
String course[]={"C","C++","Java","Python","HTML"};
JComboBox cb=new JComboBox(course);
cb.setBounds(50, 50,90,20);
add(cb);
setTitle("JComboBox Compuhelp");
setLayout(null);
setSize(350,300);
setVisible(true);
}

} //end of class Using_JComboBox

public class JComboBox_Compuhelp {
public static void main(String[] args) {

new Using_JComboBox();

}//end of main method
}//end of class JComboBox_Compuhelp

Example of JTable in Java.

import javax.swing.*;

class Using_JTable extends JFrame
{
Using_JTable()
{
String student_data[][]={ {"Ajay","34","5000"},
{"Vijay","25","4000"},
{"Sachin","30","5500"}};
String column[]={"NAME","ROLLNO","FEE"};
JTable jt=new JTable(student_data,column);
jt.setBounds(30,40,200,300);
JScrollPane sp=new JScrollPane(jt);
add(sp);
setTitle("JTable Compuhelp");
setSize(300,300);
setVisible(true);

}//end of constructor

}//end of class Using_JTable

public class JTable_Compuhelp {
public static void main(String[] args) {
new Using_JTable();

}//end of main method
}//end of class JTable_Compuhelp

Example of JList in Java.

import javax.swing.*;

class Using_JList extends JFrame
{
Using_JList()
{
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("C");
l1.addElement("C++");
l1.addElement("Java");
l1.addElement("HTML");
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
add(list);
setTitle("Jlist Compuhelp");
setSize(300,300);
setLayout(null);
setVisible(true);

}//end of Using_JList

}//end of class Using_JList

public class JList_Compuhelp {
public static void main(String[] args) {

new Using_JList();

}//end of main method

}//end of class JList_Compuhelp