Results 1 to 6 of 6
Thread: Newby
- 10-28-2010, 08:00 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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
ZoidyLast edited by Zoidy; 10-28-2010 at 09:01 PM. Reason: cut and paste error corrected ;)
- 10-28-2010, 08:38 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 10-28-2010, 08:53 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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
- 10-28-2010, 09:00 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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 ;)
- 10-28-2010, 09:24 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-06-2010, 12:11 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Newby need help with pop up banner
By Lapsesuu in forum New To JavaReplies: 0Last Post: 12-04-2008, 11:01 AM -
Java newby
By violinssoundcool in forum New To JavaReplies: 6Last Post: 01-26-2008, 05:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks