Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Hey 16 year old only started Java this morning, what is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hey 16 year old only started Java this morning, what is wrong with my code?

    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
    Last edited by Freaky Chris; November 4th, 2011 at 04:39 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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

    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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    111
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Hey 16 year old only started Java this morning, what is wrong with my code?

    Using equalsIgnoreCase method is more correct

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Hey 16 year old only started Java this morning, what is wrong with my code?

    llowe29, this thread is almost 2 years old, and the OP is probably long gone by now.

    I'm not sure why you reported Chris's post, but his answer is spot-on. Please only report posts that are spam or abuse, not posts that you (incorrectly) disagree with.

    I'm locking this thread.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is wrong with my code? JAVA-Eclipse
    By bespinoz in forum Loops & Control Statements
    Replies: 9
    Last Post: November 17th, 2012, 03:06 PM
  2. Replies: 3
    Last Post: October 19th, 2011, 11:55 PM
  3. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  4. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 01:00 AM
  5. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 07:38 PM

Tags for this Thread