Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    10
    ReputationRep:
    Hello, there
    could you help me ples?

    I am trying to do a simple calculation. But when i run the file..I keep getting this in the console

    "You were travelling at a speed of NaNm/s"

    I seriously don't know where the 'Na' came from..
    I seriously do not know why its not showing the speed like it should!

    The below is the soucre code

    import java.util.Scanner ;
    public class Test {

    public double time;
    public double speed;
    public double distance;



    public void findingSpeed(double t, double d){
    t = time ;
    d = distance ;
    speed = d/t ;
    System.out.println("You were travelling at a speed of " + speed + "m/s");

    }

    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.println("Enter you trip distance.");
    double inputd = reader.nextDouble() ;

    System.out.println("Now enter your trip duration");
    double inputt = reader.nextDouble() ;

    Test calculating = new Test ();
    calculating.findingSpeed(inputt, inputd);
    Offline

    1
    ReputationRep:
    I don't know Java but looks like it's not recognising your variable speed as a number (Not a Number). Do you have to explicitly declare speed as a double in Java ?

    Edit: quick search on primitives and seems like you don't have to do that. Maybe your inputs are not numbers ? What -exactly- are you inputting when you run the program?
    Offline

    12
    ReputationRep:
    Your assignments for time and distance should be the other way around. Right now, you're assigning the class variable time to local variable t and distance to d. You are basically calculating speed by dividing 0 by 0 because those are the default values for class doubles.
    Offline

    17
    ReputationRep:
    Switch
    t = time;
    d = distance;
    to
    time = t;
    distance = d;
    Edit: or you can omit this part completely as it's kind of redundant.
    • Community Assistant
    Offline

    20
    ReputationRep:
    Community Assistant
    My guess is that here:
    public void findingSpeed(double t, double d){
    t = time ;
    d = distance ;

    speed = d/t ;
    You're redefining d and t as "distance" and "time" which are variables that have not been initialised. Delete the ones in bold and I think it'd work, as I can't see how these variables are being used at all.

    Also your main has no } at the end but maybe you just haven't copy and pasted it in.

    It's been a while since I last used Java so I could be mistaken.
    • Thread Starter
    Offline

    10
    ReputationRep:
    THANK YOU! IT WORKED!

    I'm just new to programming..

    Btw Can u suggest Me anything I code for that would be fun meanwhile I can learn? ( I am beginner)

    I'm just doing for fun! But the video tutorial rather boring!

    Thank again <3


    Posted from TSR Mobile
    Offline

    1
    ReputationRep:
    Try searching quora for a website that encourages problem solving using code. There's a few out there.
    • Thread Starter
    Offline

    10
    ReputationRep:
    Hello friends,

    I am trying to figure out what has gone wrong in my simple calculator code for an hour now...I am getting nowhere.

    The do-while loop is not working like as it should...Its so frustrating.

    My source code ends with

    '}while( repeatVaule == repeatYes ); '

    And i keep getting error saying ' repeatVaule and repeatYest cant be resolved as a vriable.


    Here is my source code.

    Name:  Untitled-1.jpg
Views: 98
Size:  351.3 KB
    Offline

    10
    ReputationRep:
    You are declaring the "repeatVaule" and "repeatYes" inside the do-while loop. It needs to have been declared and initialized outside the loop
    Offline

    17
    ReputationRep:
    (Original post by MaleVip)
    THANK YOU! IT WORKED!

    I'm just new to programming..

    Btw Can u suggest Me anything I code for that would be fun meanwhile I can learn? ( I am beginner)

    I'm just doing for fun! But the video tutorial rather boring!

    Thank again <3


    Posted from TSR Mobile
    When I first learned Java, a project I did at the end of the year was making an ascii game of battleship (1 player and randomly placed ships).
    Offline

    2
    ReputationRep:
    (Original post by MaleVip)
    Hello friends,

    I am trying to figure out what has gone wrong in my simple calculator code for an hour now...I am getting nowhere.

    The do-while loop is not working like as it should...Its so frustrating.

    My source code ends with

    '}while( repeatVaule == repeatYes ); '

    And i keep getting error saying ' repeatVaule and repeatYest cant be resolved as a vriable.


    Here is my source code.

    Name:  Untitled-1.jpg
Views: 98
Size:  351.3 KB
    When comparing two strings, use the equals() method.
    So, instead of writing a==b,
    Write a.equals(b)

    Posted from TSR Mobile
    Offline

    11
    ReputationRep:
    (Original post by MaleVip)
    Hello, there
    could you help me ples?

    I am trying to do a simple calculation. But when i run the file..I keep getting this in the console

    "You were travelling at a speed of NaNm/s"

    I seriously don't know where the 'Na' came from..
    I seriously do not know why its not showing the speed like it should!

    The below is the soucre code

    import java.util.Scanner ;
    public class Test {

    public double time;
    public double speed;
    public double distance;



    public void findingSpeed(double t, double d){
    t = time ;
    d = distance ;
    speed = d/t ;
    System.out.println("You were travelling at a speed of " + speed + "m/s";

    }

    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.println("Enter you trip distance.";
    double inputd = reader.nextDouble() ;

    System.out.println("Now enter your trip duration";
    double inputt = reader.nextDouble() ;

    Test calculating = new Test ();
    calculating.findingSpeed(inputt, inputd);
    Stack overflow is bae
    • Thread Starter
    Offline

    10
    ReputationRep:
    Hurray!!! I did it! Thank you everyone!!!!
    Offline

    10
    ReputationRep:
    (Original post by MaleVip)
    Hurray!!! I did it! Thank you everyone!!!!
    What was the problem then?
    Offline

    10
    ReputationRep:
    (Original post by whorace)
    It's late but it appears he was assigning d and t to variables that had no value, the scanner would take in two values but it was then displaced by the reassignment

    This would work fine (renamed for better conventions)

    import java.util.Scanner ;

    public class Test {

    public void findingSpeed(double duration, double distance){
    speed = duration / distance;
    System.out.println("You travelling at a speed of " + speed + "m/s";
    }

    }

    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.println("Entertrip distance.";
    double distance = reader.nextDouble() ;
    System.out.println("Nowr your trip duration";
    double duration = reader.nextDouble() ;
    Test result = new Test ();
    result.findingSpeed(distance, duration);
    }
    I was referring to the second problem he posted
    Offline

    2
    ReputationRep:
    (Original post by ahsenrauf)
    I was referring to the second problem he posted
    I noticed and deleted my post just before you responded lmao
    • Community Assistant
    Offline

    20
    ReputationRep:
    Community Assistant
    (Original post by MaleVip)
    Btw Can u suggest Me anything I code for that would be fun meanwhile I can learn? ( I am beginner)

    Posted from TSR Mobile
    You may want to consider Project Euler
    https://projecteuler.net/
    It's a series of mathematical problems designed to be solved using a programming language of your choice. Some of the first ones are quite easy, some of the later ones are very difficult. I've only done a few if I'm honest.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Would you like to hibernate through the winter months?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.