Hey 16 year old only started Java this morning, what is wrong with my code?
Code java:
import java.util.Scanner;
class SDTCalc{
public static void main(String [] args){
Scanner jack = new Scanner(System.in);
System.out.println("Are you looking for Speed, Time or Distance? ");
String target = jack.nextLine();
double dist;
double speed;
double time;
if (target == "Speed" || target == "speed") {
System.out.println("Please enter a value for Distance (kilometres): ");
dist = jack.nextDouble();
System.out.println("Please enter a value for Time (hours): ");
time = jack.nextDouble();
System.out.println(dist/time + " km/h");
}
else if(target == "Distance" || target == "distance") {
System.out.println("Please enter a value for Speed (km/h): ");
speed = jack.nextDouble();
System.out.println("Please enter a value for Time (hours): ");
time = jack.nextDouble();
System.out.println(speed*time + " km");
}
else if(target == "Time" || target == "time") {
System.out.println("Please enter a value for Speed (km/h): ");
speed = jack.nextDouble();
System.out.println("Please enter a value for Distance (hours): ");
dist = jack.nextDouble();
System.out.println(dist/speed + " hours");
}
}
}
Appreciate all help, truly, only new but eclipse isn't showing me up any errors yet the program just won't run at all for me? Shows the first line of text then terminates.
Thanks to anyone that will give me any help with this, you truly would be helping me out big time!
Jack
EDIT: Sorry, thought it would have registered the indents too, hopefully ye can still make it out :)
Re: Hey 16 year old only started Java this morning, what is wrong with my code?
Welcome to Java Programming Forums :)
I started at an early age myself :P
I reccommend you read the following:
Announcements - What's Wrong With My Code?
it will inform you about many things including [highlight=java][/highlight]
Also please note there is an edit button, so you dont have to double post.
Now I have the technicality bits out of the way, lets get down to your problem.
the Java String class has a method called equals() which should be used for comparing two strings rather than == there are many posts on this forum discussing why, as well as on google.
Chris