Click to See Complete Forum and Search --> : Program wont compile


rhouli67
September 23rd, 2009, 08:53 PM
For some reason i get:
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:


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.

holestary
September 24th, 2009, 01:43 AM
You didn't assign a value to a and b..When you declare a variable give it a value.

ProgramThis
September 24th, 2009, 07:12 AM
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.