Archive
Archive for January 21, 2009
Java Program to print integers you have input through console using BufferedReader and StringTokenizer – Q42
January 21, 2009
Leave a comment
Q42: Java Program to print integers you have input through console using BufferedReader and StringTokenizer.
import java.util.*;
import java.io.*;
class Buf_R_Str_Token{
public static void main(String args[]) throws IOException{
BufferedReader b_r = new BufferedReader(new
InputStreamReader(System.in));
String str = b_r.readLine();
StringTokenizer st = new StringTokenizer(str, ",");
String item;
try{
while((item = st.nextToken()) != null){
System.out.print(" " + item);
}
}
catch(Exception e){
System.out.println("\n End of String.");
}
}
}
… from College notes (BCA/MCA assignments):
Categories: Java
Java File Handling, Java IO Programs
Java Program to calculate number of characters, words and lines while reading from a input stream (file or console) – Q41
January 21, 2009
Leave a comment
Q41: Java Program to calculate number of characters, words and lines while reading from a input stream (file or console)
import java.io.*;
class WordCount{
public static int words = 0;
public static int lines = 0;
public static int chars = 0;
public static void wc(InputStreamReader isr) throws IOException{
int c = 0;
boolean lastWhite = true;
String WhiteSpace = " \t\n\r";
while((c = isr.read()) != -1){
chars++;
if(c == '\n')
lines++;
int index = WhiteSpace.indexOf(c);
if(index == -1){
if(lastWhite == true){
++words;
}
lastWhite = false;
}
else
lastWhite = true;
}
if(chars != 0)
++lines;
}
public static void main(String args[]){
FileReader fr;
try{
if(args.length == 0) {
wc(new InputStreamReader(System.in));
}
else{
for(int i=0; i<args.length; i++){
fr = new FileReader(args[i]);
wc(fr);
}
}
}
catch(IOException e){
return;
}
System.out.println(lines + " " + words + " " + chars);
}
}
… from College notes (BCA/MCA assignments):
Categories: Java
Java File Handling, Java IO Programs




