import java.util.Scanner;
public class CountWords
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter string: ");
String str=sc.nextLine();
int wordCount = 1;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == ' ')
{
wordCount++;
}
}
System.out.println("Word count is = " + wordCount);
}
}
import java.util.StringTokenizer;
public class CountWordsUsingString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter string: ");
String st = sc.nextLine();
int count = 0;
StringTokenizer stk = new StringTokenizer(st, " ");
while (stk.hasMoreTokens()) {
String token = stk.nextToken();
count++;
}
System.out.println("Number of words are: " + count);
}
}
import java.util.HashMap;
import java.util.Scanner;
public class CountCharacterUsingHashMap {
public static void main(String[] args) {
// String str = "Character duplicate word example java";
Scanner sc = new Scanner(System.in);
System.out.println("Please enter string: ");
String str = sc.nextLine();
char ch1[] = str.toCharArray();
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i < ch1.length; i++)
{
char ch = ch1[i];
hm.put(ch, (hm.get(ch) == null ? 1 : hm.get(ch) + 1));
}
System.out.println("No of Characters are : " + hm.toString());
}
}
class stringreplace
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
String s = new String("swamy narayana raju");
String t = s.replace("", "\n");
System.out.println(t);
}
}
class filecreat
{
public static void main(String[] args) throws Exception
{
File f = new File("cba.txt");
System.out.println(f.exists()); // false at first time.
f.createNewFile();
System.out.println(f.exists()); // true
}
}
====================================
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Dog implements Serializable
{
transient Cat c = new Cat();
}
class Cat
{
int j = 20;
}
class SerializeDemo
{
public static void main(String arg[]) throws Exception
{
Dog d = new Dog();
System.out.println("Before Serialization:" + d);
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog) ois.readObject();
System.out.println(d1);
}
}
======================
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
class prop1
{
public static void main(String[] args)
{
try
{
Properties p = new Properties();
String pfile;
FileInputStream fis = new FileInputStream("pfile");
p.load(fis);
String sno = p.getProperty("stno");
String name = p.getProperty("sname");
String marks = p.getProperty("smarks");
System.out.println("Student number" + sno);
System.out.println("Student name" + name);
System.out.println("Student marks" + marks);
fis.close();
}
catch (ArrayIndexOutOfBoundsException ae)
{
System.err.println("plese enter properties file");
}
catch (FileNotFoundException fe)
{
System.err.println("the properties file id not found,pleace enter correct");
}
catch (Exception e)
{
System.err.println(e);
}
}
}
=====================================
import java.io.FileWriter;
class filewrit
{
public static void main(String arg[]) throws Exception
{
File f = new File("pongal.txt");
System.out.println(f.exists());// false
FileWriter fw = new FileWriter(f, true);
System.out.println(f.exists());
fw.write(97);
fw.write("run\nsoftware\n");
char[] ch1 = { 'a', 'b', 'c' };
fw.write(ch1);
fw.flush();
fw.close();
}
}
import java.io.FileReader;
class fileread
{
public static void main(String arg[]) throws Exception
{
File f = new File("pongal.txt");
FileReader fr = new FileReader(f);
System.out.println(fr.read());
char[] ch2 = new char[(int) (f.length())];
System.out.println(ch2.length);
fr.read(ch2);
for (char ch1 : ch2)
{
System.out.print(ch1);
}
}
}
package com.shris.Springboot.tutorial.config;
class EachWordReverse {
public static void main(String[] args) {
String input = "swamy good boy";
StringBuilder result = new StringBuilder();
for (String word : input.split(" ")) {
result.append(new StringBuilder(word).reverse()).append(" ");
}
System.out.println(result.toString().trim());
}
}
class AllWordsReverse {
public static void main(String[] args) {
String input = "swamy good boy";
String[] words = input.split(" ");
StringBuilder result = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
result.append(words[i]).append(" ");
}
System.out.println(result.toString().trim());
}
}
import java.util.StringTokenizer;
class StringToken {
public static void main(String[] args) {
String s = "swamy*good*boy";
StringTokenizer st = new StringTokenizer(s, "*");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
public class Token {
public static void main(String[] args) {
String s = "www.microsoft007.in";
String[] tokens = s.split("\\.");
for (String token : tokens) {
System.out.println(token);
}
}
}
import java.util.*;
import java.io.*;
class JdbcBundleTest
{
public static void main(String[] args) throws Exception
{
Properties p = new Properties();
FileInputStream fis = new FileInputStream("jdbcdata.properties");
p.load(fis);
String s1 = p.getProperty("jdbc.driver");
String s2 = p.getProperty("jdbc.url");
String s3 = p.getProperty("jdbc.user");
String s4 = p.getProperty("jdbc.pwd");
Class.forName(s1);
Connection con = DriverManager.getConnection(s2, s3, s4);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select *from sathya124");
while (rs.next())
{
System.out.println(rs.getInt(1) + "" + rs.getString(2));
}
rs.close();
stmt.close();
con.close();
fis.close();
}
}
##################################