Java help

Discussion in 'Programming & Scripting' started by x SAINT x, Feb 5, 2009 with 11 replies and 307 views.

  1. x SAINT x

    x SAINT x Enthusiast

    Messages:
    298
    Ratings:
    17
    ok so i have this code to tell you how many days you have been alive by your age...
    can some one write me the code to put it where you enter your birthday so it tells you how many days since you were born...thanks


    import java.util.Scanner;

    public class daysLived {

    public static void main(String[] args) {

    int playerNum;
    int days;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter Age to be calculated to how many days you have lived");
    playerNum = input.nextInt();
    input.close();
    days = playerNum * 365;
    System.out.println("You have been alive " + days +" days");
    }
    }

    what i have here is the basic: enter your age and it will tell you but i dont no how to make it tell you by your birthday... thanks again
     
  2. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    You'll want to subtract the current year from the birthyear and multiply it by 365, then add in however many days the month has in it and all the months leading up to it, then add in the birthday and you will have your answer (not including leap years). I'll edit with code when i come up with it

    Edit: Can you be more specific about the assignment? Like what exactly your teacher wants you to do? 'Cause if you're supposed to calculate this down to the day then this program is going to get very ugly very quickly
     
  3. OP
    x SAINT x

    x SAINT x Enthusiast

    Messages:
    298
    Ratings:
    17
    well he said we had to make it to where we could enter are birthday for example mine is 2/14/93 and when i type that it tells me the days iv been alive. i no when he said to do it i was like wtf thats gunna be hard!
     
  4. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    ****in a i had to write a separate class to calculate how many days have passed in the year for the month to have come around. whatever, I already pwnd the program i had to do for my class so I'll work on it some more tomorrow
     
    • Like Like x 1
  5. OP
    x SAINT x

    x SAINT x Enthusiast

    Messages:
    298
    Ratings:
    17
  6. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Whew that took a while, i rewrote that damn code like 5x. Anyway I had to write a separate class so the main one wouldn't be too cluttered when calculating the month values. I can show you how to put the methods into the main file if you can't have multiple classes. Go ahead and use it in your class but if you repost it anywhere please give me credit :smile:
     
  7. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    how you input the information doesn't work. You want the user to input like "5/12/1991", right? Becuase the way you do it you will get a InputMismatchException i think each time.
     
  8. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    right glad you mentioned that, you have to use spaces. like 2 / 7 / 2009, im open to suggestions if you know how to fix it
     
  9. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343
    thats wrong too lol..hold on...

    Code:
    System.out.print("Enter your birthday with slashes (no preceding 0s and full year): ");
    String input = scanner.next();
    String [] splitInput = input.split("/");
    birthMonth = Integer.parseInt(splitInput[0]);
    birthDay = Integer.parseInt(splitInput[1]);
    birthYear = Integer.parseInt(splitInput[2]);
    
    System.out.print("Enter today's date in the same format: ");
    input = scanner.next();
    splitInput = input.split("/");
    thisMonth = Integer.parseInt(splitInput[0]);
    thisDay = Integer.parseInt(splitInput[1]);
    thisYear = Integer.parseInt(splitInput[2]);
    
    there's something wrong with how you calculate the amount of days too cuz i did a year so it would in theory say 365 and it gave me 301
     
    • Like Like x 1
  10. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    wth it worked perfectly for me lol...alright back to the drawing board
     
  11. Amurka

    Amurka Contributor

    Messages:
    1,630
    Ratings:
    343

    lol somethings wrong. I added SOPs so you can see the way i input works.
     
  12. confused5

    confused5 Enthusiast

    Messages:
    766
    Ratings:
    75
    Ok I see, I think I know what the problem is. Going to have to wait til tomorrow though, tired as balls lol

    Edit: Alright I compared my result to a calculator i found online and i got the same result. Edited the attachment in my previous post...tell me if you're still have trouble