Results 1 to 3 of 3
  1. #1
    mmac1218 is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default 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

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:
    Java Code:
    totalSeconds = 0;
    I wonder why you don't declare this as int when all the previous variables have been nicely declared.

    In other situations you redeclare variables that have already been declared;
    Java Code:
    int hours;
    //.... code deleted
    int hours = Integer.parseInt(userInput); // redeclared as int here
    You can't do this. Declare hours as int once, then use it without re-declaring it:
    Java Code:
    int hours;
    //.... code deleted
    hours = Integer.parseInt(userInput);

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

Similar Threads

  1. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 03:54 AM
  2. what's wrong?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 04-14-2009, 09:07 PM
  3. So, what am I doing wrong?
    By Charles_Smith in forum New To Java
    Replies: 0
    Last Post: 10-29-2008, 02:50 PM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 11:45 AM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •