Results 1 to 6 of 6

Thread: Newby

  1. #1
    Zoidy is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Question Newby

    Hello everybody,

    I've just joined up as I've just become a computer engineering student at the Danish Technical University :)

    The studying is going well, but every once in a while I am faced with a problem I cant seem to solve... as I am face with now :p

    I am writing a program to print the date after input date from the user... i.e. if they type in 23-10-2010, the System.out.println("24-10-2010");

    Being the perfectionist I am I wish to end the 23 with rd, 24 with th, etc. I am playing with endsWith, but I cant seem to get endsWith to work with int.

    so this is what I'm playing with:

    int day;

    System.out.print("Enter the desired day (1-31): ");
    Scanner theday = new Scanner(System.in);
    day = theday.nextInt();

    // I havent included here the while loop for ensuring the correct input ranges
    // Now comes the fun part... how to add st,nd,rd or th to numbers
    // I've been thinking along the lines of:

    System.out.print(day);

    switch (day)
    case (day.endsWith(1)):
    System.out.print("st ");
    break;
    case (day.endsWith(2)):
    System.out.print("nd ");
    break;
    case (day.endsWith(3)):
    System.out.print("rd ");
    break;
    else:
    System.out.print("th ");
    break;

    // The desired result should be

    1st or 2nd or 3rd or 4th

    // But again.... I cant seem to find the correct code to obtain this result. Someone please help :p

    Best regards
    Zoidy
    Last edited by Zoidy; 10-28-2010 at 10:01 PM. Reason: cut and paste error corrected ;)

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    What about using the modulo operator to get the last digit of an int?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    The modulo operator would have my suggestion as well. But don't forget 11, 12 and 13 are special in respect to 1, 2, 3 and 21, 22 and 23!

    Furthermore, you haven't declared day. I think you meant it to be a String. But as far as I know you cannot use a String in the switch statement.

    Success,
    Erik

  4. #4
    Zoidy is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    I take it modulo is the same as modulus operator? But how can I use the modulus to get the last digit of an int when it finds a remainder?

    day is an int as required input is 1-31 (I'll upgrade original posting ;), and good point regarding 11,12, and 13.. I'll have to think that one in ;)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    But how can I use the modulus to get the last digit of an int when it finds a remainder?

    For decimal numerals the last digit is the character representing the remainder after division by 10.

  6. #6
    Zoidy is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    ok, I resolved this issue using the if else function.

    if (month==1,....)
    {
    if(day<1 || day>31)
    {
    System.out.printout("Try again");
    }
    }

    ;)

Similar Threads

  1. Newby need help with pop up banner
    By Lapsesuu in forum New To Java
    Replies: 0
    Last Post: 12-04-2008, 12:01 PM
  2. Java newby
    By violinssoundcool in forum New To Java
    Replies: 6
    Last Post: 01-26-2008, 06:28 AM

Posting Permissions

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