# Omg help!! I wrote the simplest java program and it just wont work!!! watch

1. 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) {

System.out.println("Enter you trip distance.");

Test calculating = new Test ();
calculating.findingSpeed(inputt, inputd);﻿
2. 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?
3. 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.
4. Switch
t = time;
d = distance;
to
time = t;
distance = d;
Edit: or you can omit this part completely as it's kind of redundant.
5. 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.
6. 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
7. Try searching quora for a website that encourages problem solving using code. There's a few out there.
8. 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.

9. You are declaring the "repeatVaule" and "repeatYes" inside the do-while loop. It needs to have been declared and initialized outside the loop
10. (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).
11. (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.

When comparing two strings, use the equals() method.
Write a.equals(b)

Posted from TSR Mobile
12. (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) {

System.out.println("Enter you trip distance.";

Test calculating = new Test ();
calculating.findingSpeed(inputt, inputd);﻿
Stack overflow is bae
13. Hurray!!! I did it! Thank you everyone!!!!
14. (Original post by MaleVip)
Hurray!!! I did it! Thank you everyone!!!!
What was the problem then?
15. (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) {
System.out.println("Entertrip distance.";
Test result = new Test ();
result.findingSpeed(distance, duration);﻿
}
I was referring to the second problem he posted
16. (Original post by ahsenrauf)
I was referring to the second problem he posted
I noticed and deleted my post just before you responded lmao
17. (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.

### Related university courses

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: March 1, 2016
Today on TSR

### He lied about his age

Thought he was 19... really he's 14

### University open days

Wed, 25 Jul '18
2. University of Buckingham
Wed, 25 Jul '18
3. Bournemouth University