Results 1 to 3 of 3
- 09-01-2009, 11:09 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
Please help, what am I doing wrong?
/**
* Write an application that reads values representing
* a time duration in hours, minutes, and seconds, and
* then prints the equivalent total number of seconds.
* (For example, 1 hours, 28 minutes, and 42 seconds is
* equivalent to 5322 seconds.)
*
import javax.swing.*; // include JOptionPane
public class ConvertToSeconds
{
// NOTE the use of pseudocode comments to help organize the software design!
public static void main(String[] args)
{
// 0. Declare int variables "hours", "minutes", "seconds", "totalSeconds"
int hours;
int minutes;
int seconds;
totalSeconds = 0;
// 1. Acquire duration hours from user and put the value into int variable "hours"
// Recall from slides: hours = Integer.parseInt( JOptionPane.showInputDialog(...));
String userInput = JOptionPane.showInputDialog(null, "Type in your hours: ");
int hours = Integer.parseInt(userInput);
// 2. Acquire duration minutes from user and put value into int variable "minutes"
String userInput = JOptionPane.showInputDialog(null, "Type in your minutes: ");
int minutes = Integer.parseInt(userInput);
// 3. Acquire duration seconds from user and put value into int variable "seconds"
String userInput = JOptionPane.showInputDialog(null, "Type in your seconds: ");
int seconds = Integer.parseInt(userInput);
// 4. Convert from (hours, minutes, seconds) into just seconds, and store value into the variable "totalSeconds"
// 5. Print out the value of "totalSeconds". Remember to output something like "Total seconds = 10,201"
}
// end main
}
-
You need to read up on how to declare variables and how not to declare them.
You are trying to use some variables without declaring them as in this line:
I wonder why you don't declare this as int when all the previous variables have been nicely declared.Java Code:totalSeconds = 0;
In other situations you redeclare variables that have already been declared;
You can't do this. Declare hours as int once, then use it without re-declaring it:Java Code:int hours; //.... code deleted int hours = Integer.parseInt(userInput); // redeclared as int here
Java Code:int hours; //.... code deleted hours = Integer.parseInt(userInput);
- 09-01-2009, 11:21 PM #3
Similar Threads
-
what am i doing wrong here??
By tornbacchus in forum New To JavaReplies: 19Last Post: 04-16-2009, 03:54 AM -
what's wrong?
By rayda in forum New To JavaReplies: 3Last Post: 04-14-2009, 09:07 PM -
So, what am I doing wrong?
By Charles_Smith in forum New To JavaReplies: 0Last Post: 10-29-2008, 02:50 PM -
right or wrong
By jot321 in forum New To JavaReplies: 7Last Post: 09-25-2008, 11:45 AM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks