Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner needs a little help

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
Hello all,

I'm working an a class assignment that has to do with shifting letters. Basically the premise is this: the program takes in two arguments (a char and an int) like 'a' and '3', under the assumption that the alphabet is numbered 0 to 25 for letters a to z, whatever int that is entered will shift the char's numerical value that many places to the right. Hence when 'a' and '3' is entered, 'a' becomes 'd' because a's numerical value is 0 and d is 3. I have a small test program to help me determine the numerical value of the inputted char as well as the new position of the moved char. I have the concept down but it's not compiling (most likely because I have no idea what I'm doing). The code is below, can anyone tell me what I'm doing wrong? I've put arrows at the lines where I think there is a problem.

Thanks,
Jisoo22


class Vigenere {

private char letterShift( char c, int k ){

if( !Character.isLowerCase( c ) ) return '*'; //not a lower case letter

int cint = digit( c, 25); //the position of c in alphabet (range 0-25) <----
System.out.println( &quot;should be in range 0-25:&quot; + cint );
int shift = cint + k; //the position of the new char in alphabet (range 0-25) <----
System.out.println( &quot;should be in range 0-25:&quot; + shift );
return shift; //return a char (range 'a'-'z', not 0-25)
} //letterShift


public static void main( String [] args ){
if( args.length != 2 ){
System.out.println( &quot;usage: java Vigenere char int&quot; );
return;
}
char c = args[0].charAt(0); //turn string into char
int k = Integer.parseInt( args[1] ); //turn string into int
Vigenere the_object = new Vigenere();

System.out.println( &quot;return value=&quot; + the_object.letterShift( c, k ) );
} //main

} //Tester

 
**UPDATE**

Hi again,

Ok I got it to compile and it outputs the right answer but the way it gets there is wrong. As implied before, a corresponds with &quot;0&quot;, b corresponds with &quot;1&quot; and so on. Unfortunately, the system.out.println statements I put in to see the program's progress show that when I input &quot;a&quot; as the character, it interprets it as the number &quot;97&quot;. It still outputs the letter &quot;d&quot; like I want it to, but it's just not doing it the way I need it to. The new code is below, does anyone have an idea?

Thanks,
Jisoo22


class Vigenere {

private char letterShift( char c, int k ){

if( !Character.isLowerCase( c ) ) return '*'; //not a lower case letter

int cint = c; //the position of c in alphabet (range 0-25)
System.out.println( &quot;should be in range 0-25:&quot; + cint );
int shift = cint + k; //the position of the new char in alphabet (range 0-25)
char char_a = (char) shift;
System.out.println( &quot;should be in range 0-25:&quot; + shift );
return char_a; //return a char (range 'a'-'z', not 0-25)
} //letterShift


public static void main( String [] args ){
if( args.length != 2 ){
System.out.println( &quot;usage: java Vigenere char int&quot; );
return;
}
char c = args[0].charAt(0); //turn string into char
int k = Integer.parseInt( args[1] ); //turn string into int
Vigenere the_object = new Vigenere();

System.out.println( &quot;return value=&quot; + the_object.letterShift( c, k ) );
} //main

} //Tester



input: a 3
output:
should be in range 0-25:97
should be in range 0-25:100
return value=d
Press any key to continue . . .
 
Why can't you just use the ASCII values for a-z and work on them as is currently happening i.e. a = 97, b = 98 etc.?

If you have to use your a = 0 etc then why not subtract 97 from the number you are given. Add the Shift number. Then add 97 again. Personally, I would just add your Shift number to the ASCII value you're given i.e. to change 'a' to 'd' the following calculation would take place: 97 + 3 = 100 (convert 100 back to character = 'd')

It seems pointless to do the following:
97 - 97 = 0,
0 + 3 = 3,
3 + 97 = 100!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top