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( "should be in range 0-25:" + cint );
int shift = cint + k; //the position of the new char in alphabet (range 0-25) <----
System.out.println( "should be in range 0-25:" + 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( "usage: java Vigenere char int" );
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( "return value=" + the_object.letterShift( c, k ) );
} //main
} //Tester
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( "should be in range 0-25:" + cint );
int shift = cint + k; //the position of the new char in alphabet (range 0-25) <----
System.out.println( "should be in range 0-25:" + 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( "usage: java Vigenere char int" );
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( "return value=" + the_object.letterShift( c, k ) );
} //main
} //Tester