Results 1 to 4 of 4
Like Tree3Likes
  • 1 Post By JosAH
  • 2 Post By JavaProg

Thread: 16 year old looking for help with simple Java program, help really appreciated!

  1. #1
    JavaProg is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default 16 year old looking for help with simple Java program, help really appreciated!

    Hey, I am new to Java, only two days as it happens and I am absolutely puzzled as to why this code I wrote doesn't work as it should! The idea is that the user enters whether they would like to calculate "speed", "distance" or "time". The code should then carry out different operations based on their option. (ie, for speed, it should ask for time and distance values, then calculate it based on that), elipse isn't showing up with any errors and everything runs fine up untill just before the first if statement where the program just terminates after I pick "speed", "time", "distance" or anything for that matter!

    Would greatly appreciate any help with this really wanna learn java, the eventual aim being to develop android apps! Cheers lads!









    import java.util.Scanner;


    public class apples{
    public static void main(String [] args){
    double dist;
    double time;
    double speed;

    String target;
    Scanner jack = new Scanner(System.in);

    System.out.println("Are you looking for speed, distance or time? ");
    target = jack.nextLine();

    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");

    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: 16 year old looking for help with simple Java program, help really appreciated!

    You can't compare two Strings for equality with the == operator, use the equals( ... ) method instead; as in target.equals("speed") etc.

    kind regards,

    Jos
    amaravarman likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JavaProg is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: 16 year old looking for help with simple Java program, help really appreciated!

    Hey, thanks, done this! But now it is saying "editor does not contain a main type"?
    Any idea what that is about?

    Thanks again man!
    Jack

  4. #4
    JavaProg is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: 16 year old looking for help with simple Java program, help really appreciated!

    Never mind! I got it, just made a new class and pasted it in, I probably messed around with settings or something!
    Thanks again man, will be using this forum again, it is excellent! Cheers!
    Fubarable and amaravarman like this.

Similar Threads

  1. Java need help! Help will be very much appreciated
    By paccerz in forum New To Java
    Replies: 1
    Last Post: 08-23-2011, 08:47 PM
  2. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 09:30 PM
  3. Month and year program code!!!!HELP PLEASE!!!
    By chitwood in forum New To Java
    Replies: 4
    Last Post: 10-13-2010, 01:21 PM
  4. Date of first day, given the week in the year and the year...
    By Lee.J.Baxter in forum Advanced Java
    Replies: 1
    Last Post: 08-26-2009, 08:48 AM
  5. Leap Year Program
    By busdude in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 03:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •