Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Number with subscript in Java

Sreenivas Reddy Tatikunta
Greenhorn
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.

Regards,
Sreenivas Reddy.Tatikunta

Vijitha Kumara
Bartender
Posts: 3918
10
Moved to Java in General forum.

Gamini Sirisena
Ranch Hand
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
Sheriff
Posts: 21443
33
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
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: 12234
36
The above code does not compile.

Sreenivas Reddy Tatikunta
Greenhorn
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
Posts: 50749
83
You should be able to correct the compiler error yourself with the edit button.

Gamini Sirisena
Ranch Hand
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
Posts: 50749
83
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
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
running the program it does seem to be good in the logic