The Student Room Group

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

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);
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?
(edited 8 years ago)
Reply 2
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.
Reply 3
Switch
t = time;
d = distance;
to
time = t;
distance = d;
Edit: or you can omit this part completely as it's kind of redundant.
(edited 8 years ago)
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.
Reply 5
THANK YOU! IT WORKED!

I'm just new to programming.. :frown:

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
Try searching quora for a website that encourages problem solving using code. There's a few out there.
Reply 7
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. :frown:

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.

Untitled-1.jpg
(edited 8 years ago)
You are declaring the "repeatVaule" and "repeatYes" inside the do-while loop. It needs to have been declared and initialized outside the loop
Reply 9
Original post by MaleVip
THANK YOU! IT WORKED!

I'm just new to programming.. :frown:

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).
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. :frown:

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.

Untitled-1.jpg


When comparing two strings, use the equals() method.
So, instead of writing a==b,
Write a.equals(b)

Posted from TSR Mobile
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":wink:;

}

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

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

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

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


Stack overflow is bae
Reply 12
Hurray!!! I did it! Thank you everyone!!!!
Original post by MaleVip
Hurray!!! I did it! Thank you everyone!!!!

What was the problem then?
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":wink:;
}

}

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Entertrip distance.":wink:;
double distance = reader.nextDouble() ;
System.out.println("Nowr your trip duration":wink:;
double duration = reader.nextDouble() ;
Test result = new Test ();
result.findingSpeed(distance, duration);
}


I was referring to the second problem he posted
Reply 15
Original post by ahsenrauf
I was referring to the second problem he posted


I noticed and deleted my post just before you responded lmao
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.

Quick Reply

Latest

Trending

Trending