Archive

Archive for January 20, 2009

Java Program by using PushBackReader to utilize unread method – Q40

January 20, 2009 Leave a comment

Q40: Java Program by using PushBackReader to utilize unread method.

import java.io.*;

class PB_Inp_Stream{
	public static void main(String args[]) throws IOException{
		String s = " if (a== 4) a = 0;\n";
		byte buf[] = s.getBytes();
		ByteArrayInputStream in = ByteArrayInputStream(buf);
		PushbackInputStream f = PushbackInputStream(in);
		int c;
		
		while((c = f.read()) != -1){
			switch(c){
				case '=':
					if((c = f.read()) != -1){
						System.out.print(".eq.");
					}
					else{
						System.out.print("<-");
						f.unread();
					}
					break;
				default:
					System.out.print((char) c);
					break;
			}
		}
	}
}

… from College notes (BCA/MCA assignments):


Advertisement

Java Program by using BufferedReader stream to use mark and reset methods – Q39

January 20, 2009 Leave a comment

Q39: Java Program by using BufferedReader stream to use mark and reset methods.

import java.io.*;
class Buf_Inp_Stream{
	public static void main(String args[]) throws IOException{
		String str = "This is s &copy; copyright symbol " +
					 "but this is &copy not.";
		byte buf[] = str.getBytes();
		ByteArrayInputStream in = ByteArrayInputStream(buf);
		BufferedInputStream f = new BufferedInputStream(in);
		int c;
		boolean marked = false;
		while((c = f.read()) != -1){
			switch(c){
				case '&':
					if(!marked){
						f.mark(32);
						marked = true;
					}
					else{
						marked = false;
					}
					break;
				case ';':
					if(marked){
						marked = false;
						System.out.print("(c)");
					}
					else{
						System.out.print((char) c);
					}
					break;
				case ' ':
					if(marked){
						marked = false;
						f.reset();
						System.out.print("&");
					}
					else{
						System.out.print((char) c);
					}
					break;
				default:
					if(!marked)
						System.out.print((char) c);
					break;
			}
		}
	}
}

… from College notes (BCA/MCA assignments):