Can anyone help me. I have been bangin my head on this one for two days. variable number may not have been initialized. code to enter single digit number and return largest of ten. code is as follows.

// Java packages
import javax.swing.JOptionPane;

public class Counter {

    // main method begins execution of Java application
    public static void main( String args[] )
    {
    	// declare variables
    	int counter;        // loop counter
    	String number;      // number entered by user
    	double number2 = 0;        // number converted to integer
    	double largest = 0;        // largest of all numbers entered

    	for( counter = 0; counter < 10; counter++ )

    	     number = JOptionPane.showInputDialog( "Enter a single digit number: ");

             number2 = Double.parseDouble( number );
             
             if ( number2 > number2)
             
                largest = number2;
                
                JOptionPane.showMessageDialog( null, largest + "  is the largest");
    }
    }

if ( number2 > number2)

what is this thing .... it is almost impossible .... why are you doing this ??? ... and exactly on which line is the error message , please specify.

Class variables (members) automatically initilize to a default value. Method variables do not. You did not set a value for 'number' when decalred in your 'main' method. Even though you do set it in the 'for' loop, the compiler assumes that you could possibly never enter the loop. Since this is a possibility, the compiler gives you this warning.

As a practice, I always (if possible) set the default value when decalred in a method.
;)

hi! i am gettin the same error messge when i declare an object of a class. though i have initialised it to null while declarin. pls have n look at the code n reply if u find ne of my silly mistakes or is java just wierd..

 public static void main (String argv []){
    try {
            class transform
    {String x_pos,y_pos,z_pos,width,height,x_rotation,y_rotation,z_rotation,Scalex,Scaley,Scalez;
    };
          transform t=null;
         DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File("jujups.xml"));


            // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


-----blah blah........
 NodeList textxposList = xposElement.getChildNodes();
                   [U][COLOR="Red"] t. x_pos[/COLOR][/U]=((Node)textxposList.item(0)).getNodeValue().trim();
                    System.out.println("X position : " + t.x_pos);

this is wre the error strikes n says variable t might not be initialised..

hi! i am gettin the same error messge when i declare an object of a class. though i have initialised it to null while declarin. pls have n look at the code n reply if u find ne of my silly mistakes or is java just wierd..
<snip>

Do not tack questions on the end of old threads. Start a new thread if you have a question and lose the "text speak". If you are too lazy to write actual words, then I can't see why anyone should take the time to help with your problem.

I know this is an old thread, and I'm certain the OP has already solved this problem. But for those stumbling on this problem like me, the problem is this: your for loop is lacking scope brackets. The variable is initialized within the loop and everything else is outside the loop. If a variable is initialized within a loop or try statement or whatever, it is considered to be uninitialized to anything outside of that statement.

Add scope to your loop and it should work.

Hi 'Kronk Vaesir',
try

String number = null;

and I think you meant number2 > largest

@Stewie and RWilson
even íf the problem wasn't solved by now, the OP surely doesn't really care enough any more to check this thread

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");}

    }
}

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");}

}
}

please create a new thread instead of reviving one that should have been closed over half a decade ago, it 'll make more members willing to actually read your post.

for this problem, the 'out' you're calling should be called like this:

System.out.println("Server Sent Average: "+Avg);

New thread posted here. Closing this one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.