• Post Reply Bookmark Topic Watch Topic
  • New Topic

please correct this code for me....  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**Please the code is supposed to output time in Hours, minutes and seconds when a value is entered into the prompt...please tell me what i am doing wrong in the code where there is an asterisk


import java.util.Scanner;

public class Person {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter any number > ");
double time_Input = keyboard.nextDouble();
int numberOfHours = (int) time_Input / 60;
int numberOfMinutes = (int) (time_Input % 60);
double numberOfMinutes1 = time_Input % 60;
double numberOfSeconds = numberOfMinutes1 % numberOfMinutes ;
if (time_Input>0 && time_Input<999999999)
{
System.out.printf("Time is: %d Hours, %f Minutes, and %f Seconds", numberOfHours, numberOfMinutes1, numberOfSeconds); *****something is wrong here
}else {
System.out.println("NaN");
}


}

}
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You say "something is wrong". Can you explain in more detail what exactly is wrong?

Does the code compile, or do you get an error from the compiler?

If it compiles without errors, then what happens when you run it? Do you get an exception? If yes, then copy & paste the exact message from the exception.

If there's no exception, then does it produce some other output than what you expected? What exactly did you expect, what does it actually print and how is that different from what you expected?
 
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hakim,

Learning process correctly formulate the question about the problem, usually is more important than the problem itself.
 
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"System.out.print("Please enter any number > "); "
Doesn't make any sense. isn't it? Read the question properly and try to solve it in a paper. You can identify your fault. It's a minor logic error. You can figure it out when you solve it in a white paper
 
Liutauras Vilda
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude Niroshan wrote:"System.out.print("Please enter any number > "); "
Doesn't make any sense. isn't it? Read the question properly and try to solve it in a paper. You can identify your fault. It's a minor logic error. You can figure it out when you solve it in a white paper


Jude, agree, it doesn't.
Still, no matter what he meant by writting "Please enter any number", OP's actual problem appears in his specified line.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Still, no matter what he meant by writting "Please enter any number", OP's actual problem appears in his specified line.


Yes bro.. That's why i suggest him to solve it before coding. Cos he made a logic error when he calculates the seconds. 'OP's actual problem appears in his specified line', it's just a sysout. Nothing wrong there. What matters is here the values which he got for those variables. Isn't it?
 
Liutauras Vilda
Sheriff
Posts: 4932
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, i didn't look at it carefully.
Wrong calculations.
 
Marshal
Posts: 56606
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But without OP telling us the details, e.g. what did you expect to be printed? what was printed? how can we help?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!