Answered step by step
Verified Expert Solution
Question
1 Approved Answer
find and fix the error import java.io.*; import java.net.*; class TCPclient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader
find and fix the error
import java.io.*;
import java.net.*;
class TCPclient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("Localhost", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + ' ');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
class TCPserver {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String newSentence="";
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
//here we modify the sentence
//we first need to define and initialize a boolean to check wether a letter is the first letter in a word
boolean spaceAhead = true ; // to capitalize first letter in the sentence immedietly without checking for a space ahead
//we will loop through the sentence
for (int i = 0; i
//when found a letter
if(Character.isLetter(clientSentence.charAt(i))){
//if it the first letter in a word
if(spaceAhead){
char modified = Character.toUpperCase(clientSentence.charAt(i));
newSentence+=modified;
spaceAhead = false;
}
//if it was just a part of a word (not first letter)
else{
char notmodified = Character.toUpperCase(clientSentence.charAt(i));
newSentence+=notmodified;
spaceAhead = false;
}}
//if it was not a letter (it must be a space) then change isSpace to true
char space = Character.toUpperCase(clientSentence.charAt(i));
newSentence+=space;
spaceAhead = true;
}
outToClient.writeBytes(newSentence);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started