Any body can help me with this error I am getting this error:

C:\Documents and Settings\Fuad Damra\Desktop\TCPDayTimeServer.java:33: variable out might not have been initialized
out.println("Server Sent Average: "+Avg);
^
1 error

When I am trying to execute this program:

import java.io.*;
import java.net.*;
import java.util.Date;

public class TCPDayTimeServer
{
	public static void main(String args[])
	{
		PrintWriter out;
		BufferedReader in;
		String nwkLine;
		int Sum=0;
		double Avg=0;
		int i;
		try
		{
			ServerSocket server = new ServerSocket(21);
			while(true)
			{
			for(i=1;i<=20;i++)
			{
			Socket link = server.accept();
			System.out.println("Connection Number "+i+"accepted");
			in = new BufferedReader(new InputStreamReader(link.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(link.getOutputStream()));
			String intime=in.readLine();
			int NTime = Integer.parseInt(intime);
			Sum+=NTime;
     		}//for

			Avg=Sum/i;
			out.println("Server Sent Average: "+Avg);
			out.flush();
			out.close();
	}//while

		}
		catch(IOException ioe)
		{System.out.println("io error");}

	}
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Yes, it is working now really thank you.
But i have just one last question :-)
this program will connect with 20 client and take the time from them and count the average for the time and send it back to all 20 clients but for now my program is sending the average only for the last client connect to it what i must modify to make it send the average to all clients ?

to make it send the average to all clients

Save the connections to all the clients so you can send more messages to them.
One thing you could do would be to start a Thread to handle the connection when the accept returns a socket for that connection. Have a class that saves the connection and that you can call with the messages you want sent thru the connection it is controlling. Make a list of those classes that you can iterate thru to send messages to all of them.

Edited 5 Years Ago by NormR1: n/a

aha but how to make these threads for this program my doctor ask for this but he didn't give us the threads yet so maybe you can help me with it, it will be appreciated.

I am having trouble with this also, can someone look at my code? my error is on the last line.

import java.io.*;
import java.util.*;
public class RandomIfStatements
{
    public static void main(String args[])
    {
        Scanner kbReader = new Scanner(System.in);
        System.out.println("Hello Friends, I am here to put together a short story");
        System.out.println("about you and your life");
        System.out.println("   ");
        System.out.println("What is your name? (Hint: Just your first name)");
        String a = kbReader.next();
        System.out.println("  ");
        System.out.println("How old are you?");
        String b = kbReader.next();
        System.out.println("  ");
        System.out.println("Are you male or female?");
        String e = kbReader.next();
        System.out.println("  ");
        System.out.println("Do you like to dance?");
        String RANDOMNESS = kbReader.next();
        System.out.println("Do you have any pets? Y or N?");
        String c = kbReader.next();
        System.out.println("  ");
        if (c.equals("Y"))
        {
            System.out.println("How many?");
            String d = kbReader.next();
            String yesorno;
            yesorno = "does";
            String tolol;
            if (d.equals("1"))
            {
                System.out.println("Just one? Ok, now we will move on.");
                tolol = "1";
            }
            else if (d.equals("2"))
            {
                System.out.println("Impressive, I know many cool people who have 2!");
                tolol = "2";
            }
            else if (d.equals("3"))
            {
                System.out.println("Wow! That is even more than I have! :D");
                tolol = "3";
            }
            else if (d.equals("4"))
            {
                System.out.println("You do realize that you are maxed out on pets in Denver, right?");
                tolol = "4";
            }
            else
            {
                System.out.println("You do realize that if you are in Denver you are breaking the law, right?");
                tolol = "more than 4";
            }
        }    
        else if(c.equals("N"))
        {
            System.out.println("Oh, well then, we will skip the pet part then. :(");
            String yesorno;
            yesorno = "doesn't";
        }
        else
        {
            System.out.println("Sorry, try again");
        }
        String work;
        if (e.equals("male"))
        {
            e = "he";
            c = "his";
            work = " how don't you do   this ? ";
        }
        else if (e.equals("female"))
        {
            e = "she";
            c = "her";
            work = "how do you do this?";
        }
        else
        {
            System.out.println("Not recongnized, please try again");
        }
        System.out.println("  ");
        System.out.println("Do you like this class? Y or N");
        String dkd = kbReader.next();
        String tololol;
        if (dkd.equals("Y"))
        {
            System.out.println("So do I!");
            tololol = "like";
        }
        else if (dkd.equals("N"))
        {
            System.out.println("Why not? I think that it is lots of fun!");
            tololol = "do not like";
        }
        else
        {
            System.out.println("Please Try Again");
        }
        System.out.println("  ");
        String yesorno;
        System.out.println("Please wait as I compute and process a personalized story about you...");
        System.out.println("  ");
        System.out.println("Done. Printing your personalized story now to your screen.");
        System.out.println("Hi! I know a person. "+c+" name is "+a+". "+e+" is "+b+" years old. He or she INSERT VARIABLE YESORNO have pets."+work);
    }
}
This article has been dead for over six months. Start a new discussion instead.