Re: Vari not initiliazed?
So why not give them initial values when you declare them? Also you'll want to declare each variable on its own line. Code real estate is free, so I suggest that you not be afraid to use it judiciously when it will help clarify your code.
Re: Vari not initiliazed?
Because it will be changed later in the code? Should I just set them to 1?
Re: Vari not initiliazed?
You need to either 1) set local variables (variables declared in methods, constructors or other similar blocks) to something as the compiler will not allow you to use uninitialized local variables, or 2) you need to change them to class fields in which case the compiler will automatically initialize the variables to their default values, such as 0 for ints, false for booleans, and null for reference variables.
What initial variable you use is up to you as long as it makes sense for your program.
Re: Vari not initiliazed?
Code java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Logan
*/
import java.util.Scanner;
public class area
{
public static void main(String[] args)
{
//Declaring Variables.
int triHeight, triBase, lengthRect, widthRect, shapeNum;
double circRadi;
//At this point in the code, the variables have not been initialized **
Scanner reader = new Scanner (System.in);
//Receiving Input
System.out.println ("Which shape do you want area for?");
System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
shapeNum = reader.nextInt ();
//Pretend user enters 4 for shapeNum... This is the scenario the compiler is upset over (to put it short)
if (shapeNum == 1) {// 4 != 1 **
System.out.println("Base of triangle?");
triBase = reader.nextInt ();
System.out.println("Height of triangle");
triHeight = reader.nextInt ();
}
else if (shapeNum == 2) {//4 != 2 **
System.out.println("Radius of circle?");
circRadi = reader.nextDouble ();
}
else if (shapeNum == 3) {//4 != 3 **
System.out.println ("Length of rectangle?");
lengthRect = reader.nextInt ();
System.out.println("Width of rectangle?");
widthRect = reader.nextInt ();
}
//At this point the variables still have not been initialized,
//even if the user enters 0,1,2,or3, some of the variables were not initialized, and you are trying to use them all
calculateAnswer (circRadi, triBase, lengthRect, widthRect, shapeNum, triHeight);
}
public static void calculateAnswer(double circRadi, int triHeight, int triBase, int lengthRect, int widthRect, int shapeNum){
//The compiler has seen that there is a path through the code where some the variables have not been initialized by this point
}
public static void areaTriangle(int triBase,int triHeight, int triOutput) {
triOutput = (triBase*triHeight)/2;
}
public static void areaCircle (double circOutput,int circRadi ) {
circOutput = (circRadi*Math.PI);
}
public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
rectOutput = lengthRect*widthRect;
}
}
Keep in mind that even one variable has the chance of dropping through without being initialized in some way will prevent the code from compiling.
Re: Vari not initiliazed?
Re: Vari not initiliazed?
You told me If I had a different question to make another thread. I currently have this program all finished except when it calculates the information, it will output the answer along with 2 other numbers, for example.
Code :
run:
Which shape do you want area for?
1 Triangle 2 Circle 3 Rectangle 0 None
3
Length of rectangle?
5
Width of rectangle?
10
0
3.141592653589793
50
BUILD SUCCESSFUL (total time: 5 seconds)
This is my actual code.
Code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Logan
*/
import java.util.Scanner;
public class Area {
public static void main(String[] args)
{
//Declaring Variables.
int triHeight;
int triBase;
int lengthRect;
int widthRect ;
int shapeNum;
int triOutput;
int rectOutput;
double circRadi;
double circOutput;
triHeight = 1;
triBase = 1;
lengthRect = 1;
widthRect = 1;
triOutput = 1;
circRadi = 1;
circOutput = 1;
rectOutput = 1;
Scanner reader = new Scanner (System.in);
//Receiving Input
System.out.println ("Which shape do you want area for?");
System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
shapeNum = reader.nextInt ();
if (shapeNum == 1) {
System.out.println("Base of triangle?");
triBase = reader.nextInt ();
System.out.println("Height of triangle");
triHeight = reader.nextInt ();
}
if (shapeNum == 2) {
System.out.println("Radius of circle?");
circRadi = reader.nextDouble ();
}
if (shapeNum == 3) {
System.out.println ("Length of rectangle?");
lengthRect = reader.nextInt ();
System.out.println("Width of rectangle?");
widthRect = reader.nextInt ();
}
areaTriangle (triHeight, triBase, triOutput);
areaCircle (circOutput, circRadi);
areaRectangle (rectOutput, lengthRect, widthRect);
}
public static void areaTriangle(int triBase,int triHeight, int triOutput) {
triOutput = (triBase*triHeight)/2;
System.out.println(triOutput);
}
public static void areaCircle (double circOutput,double circRadi ) {
circOutput = (Math.pow (circRadi, 2)*Math.PI);
System.out.println(circOutput);
}
public static void areaRectangle(int rectOutput, int lengthRect, int widthRect) {
rectOutput = lengthRect*widthRect;
System.out.println(rectOutput);
}
}
What am I doing wrong
Re: Vari not initiliazed?
Quote:
What am I doing wrong
Please explain what the problem is? What are you expecting the program to do? Does it do it?
If you get any error messages, copy and paste them here.
Re: Vari not initiliazed?
In my latest post, i said at the top
Quote:
I currently have this program all finished except when it calculates the information, it will output the answer along with 2 other numbers, for example.
[Code]run:
Which shape do you want area for?
1 Triangle 2 Circle 3 Rectangle 0 None
3
Length of rectangle?
5
Width of rectangle?
10
0
3.141592653589793
50
BUILD SUCCESSFUL (total time: 5 seconds)
[Code/]
It will give me the answer of 50, but the answers 3.14 and 0 are irrelevant.
Re: Vari not initiliazed?
Can you explain what the problem is? Does the program generate the desired output?
If not, please show what you want the output to be.
Quote:
the answers 3.14 and 0 are irrelevant.
What does that mean? If you don't want those numbers printed change the code so it does not print them.
When printing out numbers, it makes the output easier to understand if you include a String as an identifier. Something like:
System.out.println ("the name="+theVar);
Replace "the name" with a description of the value that is in theVar.