Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number with subscript in Java

 
Sreenivas Reddy Tatikunta
Greenhorn
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General forum.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above code does not compile.
 
Sreenivas Reddy Tatikunta
Greenhorn
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fred rosenberger,

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


Thanks & Regards,
Sreenivas Reddy.T
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to correct the compiler error yourself with the edit button.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
running the program it does seem to be good in the logic
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic