Click to See Complete Forum and Search --> : Reversing an Integer


jjohns72
11-03-2005, 06:04 AM
Hello, im new to the forum and currently taking a Intro to Java programming course. Only 2 weeks underway and I have discovered that a simple java program could assist me at work. I perform software upgrades for a t-comm company and one device that I upgrade requires you to perform an alogorithm to generate the password. Basically you take the date, reverse it, then take each number and subtract it from 10, then you take the first 3 letters of the day and add it to the end. The end result is the password for that day. Now this seems pretty simple except that I dont know how to take the integer that is inputed into the program and reverse it. Once i have that built the rest should be simple. Can anyone steer me in the right direction? Here is what I have written so far but am currently stuck......

import java.util.*;
public class IOMDecoder
{
public static void main(String[] args)
{
int firstDate, reverseDate, subtractedDate;
char dayAbrv;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the date in this format DDMMYYYY \n \n");
System.out.println("Example: November 3, 2005 would be entered as 03112005 \n \n");

firstDate = keyboard.nextInt();

BigDog
11-03-2005, 09:17 AM
You would need to parse the input, since all input like that comes in as a string, then convert the parsed tokens into actual numbers. The best place to look is the online Java API (according to the version you are using). Here is the one for 1.4.2 (one of the most commonly used) http://java.sun.com/j2se/1.4.2/docs/api/. You would want the Integer class and use the parseInt() method. Example below.


String someStringNumber = "123";
int aRealNumber;
try {
aRealNumber = Integer.parseInt(someStringNumber);
}
catch (NumberFormatException nfe) {
//do something with the exception
}

The method can throw an exception, since you have the possiblity of trying to turn a non-numeric string into a number, so you need to either handle or rethrow the exception. If someStringNumber was actually "my cat's breath smells like cat food" - that would throw an exception, since that is not a numeric string.

jjohns72
11-03-2005, 07:01 PM
Wow Oak!!! I cannot thank you enough!! That is very impressive. I dont think i ever could of gotten that with only 2 weeks into this class. I have one small favor to ask....everything is working propery except i forgot to mention one rule....when the subtraction occurs on each number if it is subtracting 0 from 10, the result cannot be a 2 digit number so has to become 0. I.E. 10-0 will become a zero....So the password for today would be 50089970thu....Sorry i failed to mention that...any ideas on how to correct that in the code? Also, im assuming that the program is pulling today's day for the 3 letter abbreviation at the end....That works great but I am curious how it accomplishes that...does it pull off of the system clock on the PC or something else? Thanks again for all you expertise and hopefully you can show me how to alter the subraction exception.

jjohns72
11-03-2005, 08:17 PM
What happened to the attachments that OAK posted? I didnt get a chance to save them yet and now they are gone???

Oak
11-04-2005, 05:39 AM
An admin must have removed them.

They could have said why... must not like gore metal or something ... :P

jjohns72
11-04-2005, 07:42 AM
The title Password generator might make them nervous, but its really just taking the date and performing some changes to it...its something I've had to do for years on paper that only takes a couple of minutes but gets really annoying after a while. Not sure Im familair with Gore Metal Oak...would GWAR be considered Gore Metal?

Oak
11-04-2005, 08:04 AM
Lol, that didnt even occur to me.

I guess I could have named it something a bit less dubious.

It was Mortician and Blood Red Throne. I think Gwar have spanned a few different genres. Havent listened to them in ages though.

Hope I helped you out and all the best.

jjohns72
11-05-2005, 12:40 AM
You have helped tremendously and I am reviewing all the coding so that I am actually learning something for my programming class although i am only in the second week and much of it is still over my head. The good news is that this is exposing me to things that I will be seeing later in the class and I will have a head start.....And for the bad news.....I tried the program tonight and i kept getting denied. After doing the equation numerous times by hand and getting the same result as the program i noticed that there is one part of the equation for the program that I forgot about...probably the most difficult.....
The 3 letter day abbreviation at the end of the password has to have one letter subtracted to each character of the abbreviation...i.e
SUN becomes RTM, MON becomes LNM, TUE becomes STD, WED becomes VDC, THU becomes SGT, FRI becomes EQH, and SAT becomes RZS. I have no idea how to subract 1 letter from a character, so I am guessing that IF statements will be needed to convert the output of the date line. Is it possible to convert the output of the day abbreviation to one letter less on each letter of the abbreviation? What do you recommend Oak?

ed81
11-05-2005, 07:23 AM
Hi guys, nice job..This is a very nice website where you can help us all the new ones to this. Can you guys please help me with mine :P, i posted yesterday. I would really appreciate.

Best Regards
Edwin

jjohns72
11-05-2005, 09:23 PM
Can anyone help me with the above mentioned problem. I need to convert the three letter day abbreviation at the end of the output to one letter less on each letter. Examples are listed above. Thanks again

Oak
11-06-2005, 01:15 PM
Yes, ofcourse :P but this is the last one.

The trick here is that you can actually use the descrement (--) and increment (++) operators on char primatives.

So its simply a case of:

char b = 'b';
char a = --b;

After the above operation, the variable a will contain 'a'.

I think you will be alright on your own from here on in.