• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Number with subscript in Java

 
Greenhorn
Posts: 24
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Java in General forum.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code does not compile.
 
Sreenivas Reddy Tatikunta
Greenhorn
Posts: 24
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
running the program it does seem to be good in the logic
 
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic