aspose file tools*
The moose likes Java in General and the fly likes Number with subscript in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Number with subscript in Java" Watch "Number with subscript in Java" New topic
Author

Number with subscript in Java

Sreenivas Reddy Tatikunta
Greenhorn

Joined: Aug 23, 2011
Posts: 24

Hello All,

Can anyone please help me out... I have a requirement like this.. I will Pass the numeric number and it should return with subscript value.

Example:-
Input :- 1,2,3,4,.......n

public String numberWithSubscript(int Num){

/*************
Logic it should return below output
***************/

}

Output :- 1st,2nd,3rd,4th,5th..21st,22nd,23rd,24th.......31st,32nd,33rd,34th......Nth.

Thanks in Advance.

Regards,
Sreenivas Reddy.Tatikunta

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

Moved to Java in General forum.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
If the pattern 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th can be established consistently (can it? what about 11?)

one way is to try a method in the Integer class and build a logic. Exceptions such as 11 will have to handled too..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

I don't see any subscripts there. (Hint: "sub"-scripts are written below the line the numbers are on.) What I do see there are ordinal numbers. Perhaps that gives you a hint, in case you wanted to do some web searching but didn't know what keywords to use.
Sreenivas Reddy Tatikunta
Greenhorn

Joined: Aug 23, 2011
Posts: 24

Hello All,

I figure out solution for this above Post... You approach with below following code.

public class NumberSubscript {

public static String NumberSubscript(int value) {
int remForHun = value % 100;
int remForTen = value % 10;
if (remForHun - remForTen == 10) {
return "th";
}
switch (remForTen) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}

public static void main(String[] args) {
NumberSubscript number = new NumberSubscript();
for (int i = 1; i <= 1000; i++) {
String st = number.NumberSubscript(i);
System.out.println(i + " Value = " + i + st);
}

}
}

Thanks & Regards,
Sreenivas Reddy.T
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

The above code does not compile.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sreenivas Reddy Tatikunta
Greenhorn

Joined: Aug 23, 2011
Posts: 24

Hello fred rosenberger,

Can you please add closing bracket(}) at the end. To complie the Class.


Thanks & Regards,
Sreenivas Reddy.T
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You should be able to correct the compiler error yourself with the edit button.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Are you sure it works? I did not go through your code thoroughly, but it looks like it will print 11st and 21st.

Is that what you want?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
11st is incorrect, and 21st is correct.
Maybe it’s better to get it to print 10th 11st 12nd 13rd 14th at first, then go back and work out the logic to correct to 11th 12th 13th later.
Sreenivas Reddy Tatikunta
Greenhorn

Joined: Aug 23, 2011
Posts: 24

Dear All,


The above logic will give the following below Output :

1st,2nd,3rd,4th.........10th,11th,12th,13th,........20th,21st,22nd,23rd,24th........nth


Gamini Sirisena @ Logic is Working good.. You can test the logic..

Campbell Ritchie @ I updated code... Just copy the class and execute it.. You will get above output..
Thanks for Providing Edit option.

Let me know if you any questions.

Thanks & Regards
Sreenivas Reddy.T
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
running the program it does seem to be good in the logic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number with subscript in Java