Hi I want to count the number of occurence of each characters in a string from a client then the server will compute the occurences and pass back to the client the result.You may have noticed that I used String for result coz im planning to send a string to client by converting the integer found string using Integer.toString().
The client will let the user input one or more sentences then when the user inputs "im done" the server will return the number occurrence of each character in the alphabet(a-z only case ignored) from all the sentences.Then when the user types "quit" close the connection and the client program. I've tried the repaceAll() then.length but it can only be used when u want to find a single char only. Is there a method in java that lets you find a particular character? then when found increment the counter for that letter.
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String sentence;
String result;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost",8001);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
boolean done = false;
while(done == false)
{
sentence = inFromUser.readLine();
if (sentence.equalsIgnoreCase("im done"))
{
outToServer.writeBytes(sentence);
result = inFromServer.readLine();
System.out.println("FROM SERVER:"+ result);
}
else if (sentence.equalsIgnoreCase("quit"))
{
done = true;
clientSocket.close();
System.out.println("Connection closed");
System.exit(0);
}
else
outToServer.writeBytes(sentence);
}
}
}
import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
String result;
String newSentence;
String sentences ="";
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
String a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1,r1,s1,t1,u1,v1,w1,x1,y1,z1;
boolean done = false;
String sentinel1 = "quit";
String sentinel2 = "im done";
ServerSocket welcomeSocket = new ServerSocket(8001);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while(done != true)
{
newSentence = inFromClient.readLine();
if (newSentence.equalsIgnoreCase(sentinel2))
{
//make a loop here to find a specific character
//missing code for counting characters here converted then to type string .
result ="a: "+ a1; b: "+b1+" c: "+c1+" d:"+d1+" e: "+e1+" f: "+f1+" g: "+g1+" h:"+h1+" i: "+i1+"\nj: "+ j1 +" k: "+k1+" l: "+l1+" m: "+m1+" n: "+n1+" o: "+o1+" p: "+p1+" q:"+q1+" r: "+r1+"\ns: "+ s1 +" t: "+t1+" u: "+u1+" v: "+v1+" w: "+w1+" x: "+x1+" y: "+y1+" z:"+z1;
outToClient.writeBytes(result);
sentences = "";
}
else if (newSentence.equalsIgnoreCase(sentinel1))
{
done = true;
inFromClient.close();
outToClient.close();
}
else
sentences = sentences + newSentence;
}
}
}
}