CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2009
    Posts
    1

    Program wont compile

    For some reason i get:
    Code:
     ryan@ryan-desktop:~/Documents/Programs/Java_Programming/Labs_1068/Lab_4$ javac Server.java
    Server.java:78: variable a might not have been initialized
    					System.out.println("gcd(" + a + ", " + b + ") = " + Client.gcd(a, b));	
    					                            ^
    Server.java:78: variable b might not have been initialized
    					System.out.println("gcd(" + a + ", " + b + ") = " + Client.gcd(a, b));	
    					                                       ^
    Server.java:106: variable a might not have been initialized
    					System.out.println("MaxFactor(" + a + ", " + b + ") = " + Client.maxFactor(a, b));
    					                                  ^
    Server.java:106: variable b might not have been initialized
    					System.out.println("MaxFactor(" + a + ", " + b + ") = " + Client.maxFactor(a, b));
    					                                             ^
    Server.java:131: variable a might not have been initialized
    					Client.fibonaci(a);
    					                ^
    Server.java:156: variable a might not have been initialized
    					System.out.println("aPower(" + a + ") = " + Client.aPower(a));
    					                               ^
    Server.java:181: variable a might not have been initialized
    					System.out.println("aPowerOf2(" + a + ") = " + Client.aPowerOf2(a));
    When i try to compile this:

    Code:
    import java.util.*;
    
    public class Server
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    		int Case = 0;
    		int Counter = 1;
    		int Counter_2 = 1;
    		int Counter_3 = 1;
    		int a, b, m;
    
    		System.out.println("Welcome to the Math Wizard!");
    		while(Counter == 1)
    		{
    			System.out.println();
    			System.out.println("If you would like to find the greatest common divisor ENTER 1.");
    			System.out.println("If you would like to find the Max Factor ENTER 2.");
    			System.out.println("If you would like to find the Fibonaci sequence ENTER 3.");
    			System.out.println("If you would like to find what your number is the power of ENTER 4.");
    			System.out.println("If you would like to find out what power of two your number is ENTER 5.");
    			System.out.println("If you would like to leave the program now ENTER 6.\n");
    			
    			Counter_2 = 1;
    			Counter_3 = 1;
    
    			while(Counter_2 == 1)	
    			{						
    				try
    				{	
    					System.out.print("Please enter your number: ");		
    					Case = scan.nextInt();
    					m = 0;
    				}
    				catch(InputMismatchException e)
    				{
    					scan.next();					
    					System.out.println("Not a numeric character!");
    					System.out.println("Please try again!");
    					m = 1;
    				}
    				if(m == 1)
    					;
    				else
    					Counter_2 = 2;
    			}
    		
    			switch(Case)
    			{
    				case 1:
    				{
    					while(Counter_3 == 1)
    					{
    						try
    						{
    							System.out.println("Please enter two numbers: ");
    							System.out.print("a = ");
    							a = scan.nextInt();
    							System.out.print("b = ");
    							b = scan.nextInt();
    							m = 0;
    						}
    						catch(InputMismatchException e)
    						{
    							scan.next();					
    							System.out.println("Not a numeric character!");
    							System.out.println("Please try again!");
    							m = 1;
    						}
    						if(m == 1)
    							;
    						else
    							Counter_2 = 2;
    					}			
    					System.out.println("gcd(" + a + ", " + b + ") = " + Client.gcd(a, b));	
    					break;
    				}
    				case 2:
    				{
    					while(Counter_3 == 1)
    					{
    						try
    						{
    							System.out.println("Please enter two numbers: ");
    							System.out.print("a = ");
    							a = scan.nextInt();
    							System.out.print("b = ");
    							b = scan.nextInt();
    							m = 0;
    						}
    						catch(InputMismatchException e)
    						{
    							scan.next();					
    							System.out.println("Not a numeric character!");
    							System.out.println("Please try again!");
    							m = 1;
    						}
    						if(m == 1)
    							;
    						else
    							Counter_2 = 2;
    					}	
    					System.out.println("MaxFactor(" + a + ", " + b + ") = " + Client.maxFactor(a, b));
    					break;
    				}
    				case 3:
    				{				
    					while(Counter_3 == 1)
    					{
    						try
    						{
    							System.out.print("Please enter one number: ");
    							a = scan.nextInt();
    							m = 0;
    						}
    						catch(InputMismatchException e)
    						{
    							scan.next();					
    							System.out.println("Not a numeric character!");
    							System.out.println("Please try again!");
    							m = 1;
    						}
    						if(m == 1)
    							;
    						else
    							Counter_2 = 2;
    					}	
    					Client.fibonaci(a);
    					break;
    				}
    				case 4:
    				{
    					while(Counter_3 == 1)
    					{
    						try
    						{
    							System.out.print("Please enter one number: ");
    							a = scan.nextInt();
    							m = 0;
    						}
    						catch(InputMismatchException e)
    						{
    							scan.next();					
    							System.out.println("Not a numeric character!");
    							System.out.println("Please try again!");
    							m = 1;
    						}
    						if(m == 1)
    							;
    						else
    							Counter_2 = 2;
    					}	
    					System.out.println("aPower(" + a + ") = " + Client.aPower(a));
    					break;
    				}
    				case 5:
    				{
    					while(Counter_3 == 1)
    					{
    						try
    						{
    							System.out.print("Please enter one number: ");
    							a = scan.nextInt();
    							m = 0;
    						}
    						catch(InputMismatchException e)
    						{
    							scan.next();					
    							System.out.println("Not a numeric character!");
    							System.out.println("Please try again!");
    							m = 1;
    						}
    						if(m == 1)
    							;
    						else
    							Counter_2 = 2;
    					}	
    					System.out.println("aPowerOf2(" + a + ") = " + Client.aPowerOf2(a));
    					break;
    				}
    				default:
    				{
    					System.out.println("\nThank you for using Math Wizard!");
    					System.out.println("Have a nice day!");
    					Counter = 2;
    					break;
    				}
    			}	
    		}	
    	}
    }
    Any help would be greatly appreciated. Thank you.

  2. #2
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: Program wont compile

    You didn't assign a value to a and b..When you declare a variable give it a value.

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  3. #3
    Join Date
    Feb 2008
    Posts
    966

    Re: Program wont compile

    Just to elaborate a little more on the answer given by holestary, class level variables (primitives at least) are initialized when an instance of the class is created, but local variables are not.

    The error/warning that the compiler is giving you tells you everything that you need to know. I don't say this to be rude, but you really need to learn how to read the error messages that the Java compiler gives you. It tells you what line (java:78), tells you what the error is (variable a might not be initialized) and it shows you with an illustration exactly where the problem is. Understanding the error messages will help you go a long way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured