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

# Coding with the help of algorithm

Ranch Hand
Posts: 114
1
• Number of slices to send:
Optional 'thank-you' note:
There is a String that contains numbers.
I need to replace the numbers from 100 to 775 with 'AA', 'AB', .., 'ZZ'.

A rough logic :

• Substract 100 resulting in a number from 0 to 675 (= 26 * 26 - 1).

• Divide the number by 26 resulting in a quotient less then 26 and the remainder (less than 26 by definition)
• Use CHAR table to store characters {'A', 'B'...} and then use quotient and remainder as index to determine the 2 characters to use.

• Any help in writing the code will be appreciated.

Bartender
Posts: 4568
9
• Number of slices to send:
Optional 'thank-you' note:
One thing that can help you get the character to use is the fact that char values are actually really integers. That means that you don't need a lookup table. You can do things like this:
If n = 0, c = 'A'. If n = 25, c = 'Z'.

The other thing that will be useful are some of the methods in the java.lang.String class, which can be used for replacing substrings.

See how far you can get with that to writing the code you need, and if you have any specific problems we can help you out. But you should have a go yourself first.

lowercase baba
Posts: 13089
67
• 1
• Number of slices to send:
Optional 'thank-you' note:

start small
compile often
test a LOT
write lots of methods
compile often
test a LOT
don't be afraid to write code you'll later throw away
compile often
test a LOT
break the problem down into small, simple methods
compile often
test a LOT

you may notice that i repeated a few. That's because they are THE MOST IMPORTANT and need to be emphasized.

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Tiya Khambadkone wrote:Any help in writing the code will be appreciated.

Sounds to me like you've done all the hard work because you have a very clear and concise explanation of the logic - something that MANY beginners forget to do, so: well done.

Now you simply need to write Java code that conforms to it (and that's where Fred's advice comes in).

The rule here is that we like you to ShowSomeEffort (←click), so my suggestion is that you write some code based on your explanation and come back if you run into problems; we'll be more than happy to help.

About the only other thing I can advise: Make sure that your program deals with situations that are outside the norm - for example, if you get a number that is < 0 or > 775. Anyone can write a program that works when everything is OK; good programmers write ones that work when they aren't.

Winston

Ranch Hand
Posts: 114
1
• Number of slices to send:
Optional 'thank-you' note:
I wrote the following code :

I still feel, it needs to be refined especially at step 3. Any thoughts?

Winston Gutkowski
Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Tiya Khambadkone wrote:I still feel, it needs to be refined especially at step 3. Any thoughts?

Well, a couple leap out at me:

1. You need to convert this number this number to a String. Specifically, you need to convert numbers in the range 100-775 to one containing two letters. So how about writing a method that takes a number and returns a String? viz:that way, you remove all that code from your if block; and (probably even more importantly) from main().

It's a good rule to learn: whenever you have a major piece of logic - particularly if you can give it a name - put it in a method. You'll find that it makes your program much easier to read and follow.

Now your main() code might look something like:
Alternatively (and possibly even better), you could write another method that includes the entire conversion process and calls convertToLetters() itself (ie, have two methods). Then your main() code becomes something like:
String numberString = convert(number);

And what could be easier to read than that?

PS: Note that the conversion method is private. It's only for OUR internal use, not everybody's.

2. Lines 9-12 are redundant. You can just write:
int q = number/26;
int r = number%26;

HIH

Winston

PS: I broke up some of those enormous lines of yours. Please DontWriteLongLines. Thanks.