Can someone please help me with a number conversion program?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Can someone please help me with a number conversion program?

  1. #1
    Join Date
    Apr 2003
    Posts
    3

    Unhappy Can someone please help me with a number conversion program?

    I have to design a number conversion program for Uni.
    Basically, what I have to do is create a program that will convert a four digit Arabic numeral of a year (eg. 1988) into Roman numerals (eg. MCMLXXXVIII).
    I am completely stumped. I will be eternally grateful to anyone who can help me.
    Thank you!

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Do a google search. There are many known solutions.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Apr 2003
    Posts
    3
    Thanks for the advice ArchAngel!
    I googled it and came up with some example source code that I couldn't understand.

    Last night I came up with my own (written english) algorithm which was quite simple. The program I need to write from this is fairly simple (well...for the average Java user - not me of course ), but I need help getting it started.

    Could someone please design a small program where the user is prompted to enter two single digit numbers (0-9). If the user enters [1], then the program should print "A", if they enter [2] then it should print "B", if they enter [3] then it should print "C" and so on. I was hoping that the consecutinve letters would be printed next to each other without spaces.

    So if the user enters 1 as the first digit, then 2 as the second, I want the program to output the letters "AB".

    If you could do this for me I could improvise from there and complete the program. I seem to be better at learning how to program by imitating others
    Thank You.

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    If want people to reply, it's generally best to ask a specific question, rather than ask them to write a program. But, I'm feeling rather chirpy today, so here it is:

    Code:
    import java.io.*;
    
    public class Int2Str {
            public static void main(String[] args) throws IOException {
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Enter 1st Number: ");
                    String line1 = br.readLine();
                    System.out.print("Enter 2nd Number: ");
                    String line2 = br.readLine();
    
                    // Validate the input - I'll leave that to you and assume everything's OK.
    
                    int indexBeforeA = 'A' - 1;
    
                    // Calculate the appropriate Unicode index
    
                    int i1 = indexBeforeA + Integer.parseInt(line1);
                    int i2 = indexBeforeA + Integer.parseInt(line2);
    
                    // Convert the integers to chars
    
                    char c1 = (char)i1;
                    char c2 = (char)i2;
    
                    System.out.println("Output: " + c1 + c2);
            }
    }
    The only bit of code in this that you might find a little strange should be:
    Code:
                    int indexBeforeA = 'A' - 1;
    
                    // Calculate the appropriate Unicode index
    
                    int i1 = indexBeforeA + Integer.parseInt(line1);
    Put simply, when you cast an int to a char, it uses the int value as an index into a unicode table of characters. 'A' == 45 (I think). Because you wanted 'A' to be output if the user entered '0', I had to do a little bit of maths to offset the input.

    Hope this helps,

    ArchAngel.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Apr 2003
    Posts
    3

    Thumbs up

    Thank you so much for that!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center