Hello, I am having a problum working on a project. how do I initialized a something that I do not want a varaiable to effect.

import java.lang.*;
import java.util.Scanner;
public class HW05_03 {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int angleInDeg;
		double angleInRadians = Math.toRadians(angleInDeg);
		
		
		// prompted the user
		System.out.println("Enter an angle: ");
		angleInDeg = stdIn.nextInt();
		
		System.out.println("sin(Deg) = " + Math.sin(angleInRadians));
		System.out.println("cos(Deg) = " + Math.cos(angleInRadians));
		System.out.println("tan(Deg) = " + Math.tan(angleInRadians));
		
		System.out.println();

	}

}

this is the error I get.


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable angleInDeg may not have been initialized

at HW05_03.main(HW05_03.java:15)

int angleInDeg; // this is an unitialised variable...
double angleInRadians = Math.toRadians(angleInDeg); // ... which is used here

You define angleInDeg then immediately try to pass its value to a method. Obviously that's a mistake. What were you expecting that method call to do in that position?

int angleInDeg; // this is an unitialised variable...
double angleInRadians = Math.toRadians(angleInDeg); // ... which is used here

You define angleInDeg then immediately try to pass its value to a method. Obviously that's a mistake. What were you expecting that method call to do in that position?

it is ment to ask the user for an angle and it should return the SIN COS and TAN

So you have the right statements, but they are in the wrong order!

1. ask the user for angle in degrees
2. convert to radians
3. calculate & display sin/cos/tan

You are treating line 9 as if it is some kind of definition of the relationship between angleInDeg and angleInRadians. That's a nice idea, and something like that is scheduled to be added into Java. Unfortunately that's not how Java works now. Line 9 is a simple executable statement that will be processed just once, according to its position in the code. So it has to go after you have got the angle in degrees.

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.