Using only alphanumeric characters(a-z) inside toCharArray
Posted
by
Aaron
on Stack Overflow
See other posts from Stack Overflow
or by Aaron
Published on 2012-09-08T17:44:28Z
Indexed on
2012/09/15
15:38 UTC
Read the original article
Hit count: 353
Below you will find me using toCharArray in order to send a string to array. I then MOVE the value of the letter using a for statement...
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
However, when I use shiftCode to move the value such as...
a shifted by -1; I get a symbol @. Is there a way to send the string to shiftCode or tell shiftCode to ONLY use letters? I need it to see my text, like "aaron", and when I use the for statement iterate through a-z only and ignore all symbols and numbers. I THINK it is as simple as...
letter=codeWord.toCharArray(a,z);
But trying different forms of that and googling it didn't give me any results. Perhaps it has to do with regex or something? Below you will find a complete copy of my program; it works exactly how I want it to do; but it iterates through letters and symbols. I also tried finding instructions online for toCharArray but if there exists any arguments I can't locate them.
My program...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* Aaron L. Jones
* CS219
* AaronJonesProg3
*
* This program is designed to -
* Work as a Ceasar Cipher
*/
/**
*
* Aaron Jones
*/
public class AaronJonesProg3 {
static String codeWord;
static int shiftCode;
static int i;
static char[] letter;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// Instantiating that Buffer Class
// We are going to use this to read data from the user; in buffer
// For performance related reasons
BufferedReader reader;
// Building the reader variable here
// Just a basic input buffer (Holds things for us)
reader = new BufferedReader(new InputStreamReader(System.in));
// Java speaks to us here / We get it to query our user
System.out.print("Please enter text to encrypt: ");
// Try to get their input here
try {
// Get their codeword using the reader
codeWord = reader.readLine();
// Make that input upper case
codeWord = codeWord.toUpperCase();
// Cut the white space out
codeWord = codeWord.replaceAll("\\s","");
// Make it all a character array
letter = codeWord.toCharArray();
}
// If they messed up the input we let them know here and end the prog.
catch(Throwable t) {
System.out.println(t.toString());
System.out.println("You broke it. But you impressed me because"
+ "I don't know how you did it!");
}
// Java Speaks / Lets get their desired shift value
System.out.print("Please enter the shift value: ");
// Try for their input
try {
// We get their number here
shiftCode = Integer.parseInt(reader.readLine());
}
// Again; if the user broke it. We let them know.
catch(java.lang.NumberFormatException ioe) {
System.out.println(ioe.toString());
System.out.println("How did you break this? Use a number next time!");
}
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
System.out.println();
/****************************************************************
****************************************************************
***************************************************************/
// Java speaks to us here / We get it to query our user
System.out.print("Please enter text to decrypt: ");
// Try to get their input here
try {
// Get their codeword using the reader
codeWord = reader.readLine();
// Make that input upper case
codeWord = codeWord.toUpperCase();
// Cut the white space out
codeWord = codeWord.replaceAll("\\s","");
// Make it all a character array
letter = codeWord.toCharArray();
}
// If they messed up the input we let them know here and end the prog.
catch(Throwable t) {
System.out.println(t.toString());
System.out.println("You broke it. But you impressed me because"
+ "I don't know how you did it!");
}
// Java Speaks / Lets get their desired shift value
System.out.print("Please enter the shift value: ");
// Try for their input
try {
// We get their number here
shiftCode = Integer.parseInt(reader.readLine());
}
// Again; if the user broke it. We let them know.
catch(java.lang.NumberFormatException ioe) {
System.out.println(ioe.toString());
System.out.println("How did you break this? Use a number next time!");
}
for(i = 0; i < letter.length; i++){
letter[i] += (shiftCode);
System.out.print(letter[i]);
}
System.out.println();
}
}
© Stack Overflow or respective owner