Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Making a program that calculates how many days you lived.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Making a program that calculates how many days you lived.

    This is another code(below) from my CS Assignment. Here, I have to build a program that calculates how many days have you lived and how many hours you slept. I have seem to got the question but I get an error with Scanner sc = new Scanner(System.in);

    Here is the code:
          import java.util.*;
       public class DaysLived {
          public static void main(String args [] ){
             Scanner sc = new Scanner(System.in);
             int by;
             int cy;
             int mb;
             int mc;
             int db;
             int dc;
             int sleep;
             int aliveday;
             aliveday = ((cy-by)*365)+((mc-mb)*30)+(dc-db);
             sleep = aliveday*8;
     
             System.out.println("What's the date today?");
             System.out.print ("Year: ");  
             cy = sc.nextInt ();
             System.out.print ("Month: ");
             mc = sc.nextInt ();
             System.out.print ("Day: ");
             dc = sc.nextInt ();
             System.out.println ("Great, when were you born?");
             System.out.print ("Year: ");  
             by = sc.nextInt ();
             System.out.print ("Month: ");
             mb = sc.nextInt ();
             System.out.print ("Day: ");
             db = sc.nextInt ();
             System.out.println ("You have lived " + aliveday + "and you have slept " + sleep + " hours.");
          }
       }


    So, I am trying to get an input from the user.

    What should I do?

    Thanks in advance.
    Last edited by shifat96; February 9th, 2012 at 07:42 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Making a program that calculates how many days you lived.

    but I get an error with Scanner sc = new Scanner(System.in);
    What should I do?
    You should certainly copy and paste the full text of any errors you see.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a program that calculates how many days you lived.

    Here are the errors I got:

    variable aliveday may have not been initialized
    variable sleep may have not been initialized
    valu cannot be used ( on sleep = aliveday*8; )

    Quote Originally Posted by Sean4u View Post
    You should certainly copy and paste the full text of any errors you see.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Making a program that calculates how many days you lived.

    And now if you look carefully over the errors description, you will hopefully, get the answers yourself. Do as your compiler is asking you to do.

Similar Threads

  1. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  2. So I'm making this program...
    By SkyAphid in forum The Cafe
    Replies: 2
    Last Post: August 29th, 2011, 05:55 PM
  3. Very basic program but its making me crazy
    By craig carl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 03:16 PM
  4. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  5. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM