Looking to translate a character into an integer for use with awt robots key controls. I have this solution which works right now but its long, messy, and I'll need to add every single possible key that could be entered, which is a lot. Is there a simpler or neater way to complete this task? Thanks!
(I'm using +10000 for capital letters right now, I'll just run a check while I'm typing the chars and say if num>10000, hold shift and remove 10000 from the number)
public int charToNum(char character) {
switch (character) {
case 'a': return(KeyEvent.VK_A);
case 'b': return(KeyEvent.VK_B);
case 'c': return(KeyEvent.VK_C);
case 'd': return(KeyEvent.VK_D);
case 'e': return(KeyEvent.VK_E);
case 'f': return(KeyEvent.VK_F);
case 'g': return(KeyEvent.VK_G);
case 'h': return(KeyEvent.VK_H);
case 'i': return(KeyEvent.VK_I);
case 'j': return(KeyEvent.VK_J);
case 'k': return(KeyEvent.VK_K);
case 'l': return(KeyEvent.VK_L);
case 'm': return(KeyEvent.VK_M);
case 'n': return(KeyEvent.VK_N);
case 'o': return(KeyEvent.VK_O);
case 'p': return(KeyEvent.VK_P);
case 'q': return(KeyEvent.VK_Q);
case 'r': return(KeyEvent.VK_R);
case 's': return(KeyEvent.VK_S);
case 't': return(KeyEvent.VK_T);
case 'u': return(KeyEvent.VK_U);
case 'v': return(KeyEvent.VK_V);
case 'w': return(KeyEvent.VK_W);
case 'x': return(KeyEvent.VK_X);
case 'y': return(KeyEvent.VK_Y);
case 'z': return(KeyEvent.VK_Z);
case 'A': return(KeyEvent.VK_A);
case 'B': return(10000 + KeyEvent.VK_B);
case 'C': return(10000 + KeyEvent.VK_C);
case 'D': return(10000 + KeyEvent.VK_D);
case 'E': return(10000 + KeyEvent.VK_E);
case 'F': return(10000 + KeyEvent.VK_F);
case 'G': return(10000 + KeyEvent.VK_G);
case 'H': return(10000 + KeyEvent.VK_H);
case 'I': return(10000 + KeyEvent.VK_I);
case 'J': return(10000 + KeyEvent.VK_J);
case 'K': return(10000 + KeyEvent.VK_K);
case 'L': return(10000 + KeyEvent.VK_L);
case 'M': return(10000 + KeyEvent.VK_M);
case 'N': return(10000 + KeyEvent.VK_N);
case 'O': return(10000 + KeyEvent.VK_O);
case 'P': return(10000 + KeyEvent.VK_P);
case 'Q': return(10000 + KeyEvent.VK_Q);
case 'R': return(10000 + KeyEvent.VK_R);
case 'S': return(10000 + KeyEvent.VK_S);
case 'T': return(10000 + KeyEvent.VK_T);
case 'U': return(10000 + KeyEvent.VK_U);
case 'V': return(10000 + KeyEvent.VK_V);
case 'W': return(10000 + KeyEvent.VK_W);
case 'X': return(10000 + KeyEvent.VK_X);
case 'Y': return(10000 + KeyEvent.VK_Y);
case 'Z': return(10000 + KeyEvent.VK_Z);
case '!': return(KeyEvent.VK_EXCLAMATION_MARK);
case ' ': return(KeyEvent.VK_SPACE);
default: System.out.println("Test"); return 0;
}
}
Looking at the source code the values for the KeyEvent fields are
public static final int VK_A = 65;
public static final int VK_B = 66;
...
public static final int VK_Z = 90;
which are just ascii values for A-Z. You can cast the char to an int to get its ascii value which means you could do something like this.
public static int charToNum(char inputChar) {
if(inputChar == '!') {
return(KeyEvent.VK_EXCLAMATION_MARK);
} else if (inputChar == ' ') {
return(KeyEvent.VK_SPACE);
}
if (Character.isUpperCase(inputChar)) {
return (int) inputChar + 10000;
} else {
return (int) inputChar - 32;
}
}
however any non alphabet character will have to have its own special case like the exclamation mark, and space.
Related
I can't seem to make the Robot .keypress/keyrelease methods from Java to work inside a Virtual Machine when I'm not connected to that virtual machine.
It works as expected when I'm connected to the VM using the remote connection from my station.
The idea is I want to be able to use send a sequence of keys that represent a path e.g. C://myfile.txt inside a window upload frame like the following picture:
enter image description here
I have the following code
public class Keyboard {
private Robot robot;
public Keyboard() throws AWTException {
this.robot = new Robot();
}
public void type(CharSequence characters) {
int length = characters.length();
for (int i = 0; i < length; i++) {
char character = characters.charAt(i);
type(character);
}
}
public void type(char character) {
switch (character) {
case 'a': doType(VK_A); break;
case 'b': doType(VK_B); break;
case 'c': doType(VK_C); break;
case 'd': doType(VK_D); break;
case 'e': doType(VK_E); break;
case 'f': doType(VK_F); break;
case 'g': doType(VK_G); break;
case 'h': doType(VK_H); break;
case 'i': doType(VK_I); break;
case 'j': doType(VK_J); break;
case 'k': doType(VK_K); break;
case 'l': doType(VK_L); break;
case 'm': doType(VK_M); break;
case 'n': doType(VK_N); break;
case 'o': doType(VK_O); break;
case 'p': doType(VK_P); break;
case 'q': doType(VK_Q); break;
case 'r': doType(VK_R); break;
case 's': doType(VK_S); break;
case 't': doType(VK_T); break;
case 'u': doType(VK_U); break;
case 'v': doType(VK_V); break;
case 'w': doType(VK_W); break;
case 'x': doType(VK_X); break;
case 'y': doType(VK_Y); break;
case 'z': doType(VK_Z); break;
case 'A': doType(VK_SHIFT, VK_A); break;
case 'B': doType(VK_SHIFT, VK_B); break;
case 'C': doType(VK_SHIFT, VK_C); break;
case 'D': doType(VK_SHIFT, VK_D); break;
case 'E': doType(VK_SHIFT, VK_E); break;
case 'F': doType(VK_SHIFT, VK_F); break;
case 'G': doType(VK_SHIFT, VK_G); break;
case 'H': doType(VK_SHIFT, VK_H); break;
case 'I': doType(VK_SHIFT, VK_I); break;
case 'J': doType(VK_SHIFT, VK_J); break;
case 'K': doType(VK_SHIFT, VK_K); break;
case 'L': doType(VK_SHIFT, VK_L); break;
case 'M': doType(VK_SHIFT, VK_M); break;
case 'N': doType(VK_SHIFT, VK_N); break;
case 'O': doType(VK_SHIFT, VK_O); break;
case 'P': doType(VK_SHIFT, VK_P); break;
case 'Q': doType(VK_SHIFT, VK_Q); break;
case 'R': doType(VK_SHIFT, VK_R); break;
case 'S': doType(VK_SHIFT, VK_S); break;
case 'T': doType(VK_SHIFT, VK_T); break;
case 'U': doType(VK_SHIFT, VK_U); break;
case 'V': doType(VK_SHIFT, VK_V); break;
case 'W': doType(VK_SHIFT, VK_W); break;
case 'X': doType(VK_SHIFT, VK_X); break;
case 'Y': doType(VK_SHIFT, VK_Y); break;
case 'Z': doType(VK_SHIFT, VK_Z); break;
case '`': doType(VK_BACK_QUOTE); break;
case '0': doType(VK_0); break;
case '1': doType(VK_1); break;
case '2': doType(VK_2); break;
case '3': doType(VK_3); break;
case '4': doType(VK_4); break;
case '5': doType(VK_5); break;
case '6': doType(VK_6); break;
case '7': doType(VK_7); break;
case '8': doType(VK_8); break;
case '9': doType(VK_9); break;
case '-': doType(VK_MINUS); break;
case '=': doType(VK_EQUALS); break;
case '~': doType(VK_BACK_QUOTE); break;
case '!': doType(VK_SHIFT, VK_EXCLAMATION_MARK); break;
case '#': doType(VK_SHIFT, VK_AT); break;
case '#': doType(VK_SHIFT, VK_NUMBER_SIGN); break;
case '$': doType(VK_SHIFT, VK_DOLLAR); break;
case '%': doType(VK_SHIFT, VK_5); break;
case '^': doType(VK_SHIFT, VK_CIRCUMFLEX); break;
case '&': doType(VK_SHIFT, VK_AMPERSAND); break;
case '*': doType(VK_SHIFT, VK_ASTERISK); break;
case '(': doType(VK_LEFT_PARENTHESIS); break;
case ')': doType(VK_RIGHT_PARENTHESIS); break;
case '_': doType(VK_SHIFT, VK_UNDERSCORE); break;
case '+': doType(VK_SHIFT, VK_PLUS); break;
case '\t': doType(VK_TAB); break;
case '\n': doType(VK_ENTER); break;
case '[': doType(VK_OPEN_BRACKET); break;
case ']': doType(VK_CLOSE_BRACKET); break;
case '\\': doType(VK_BACK_SLASH); break;
case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
case ';': doType(VK_SEMICOLON); break;
case ':': doType(VK_SHIFT, VK_SEMICOLON); break;
case '\'': doType(VK_QUOTE); break;
case '"': doType(VK_SHIFT, VK_QUOTEDBL); break;
case ',': doType(VK_COMMA); break;
case '<': doType(VK_SHIFT, VK_COMMA); break;
case '.': doType(VK_PERIOD); break;
case '>': doType(VK_SHIFT, VK_PERIOD); break;
case '/': doType(VK_SLASH); break;
case '?': doType(VK_SHIFT, VK_SLASH); break;
case ' ': doType(VK_SPACE); break;
case '\b': doType(VK_BACK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
}
}
private void doType(int... keyCodes) {
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length) {
if (length == 0) {
return;
}
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
And the way i use this is:
Keyboard keyboard = new Keyboard();
keyboard.type(path);
In my project, we need to extract a file from a password protected 7zip archive.
My source code as following:
try {
ProcessBuilder pb = new ProcessBuilder("7z", "e", "bootstrap.7z", "-so", "bootstrap.txt");
Process p = pb.start();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
writer.write("password" + "\n");
writer.flush();
writer.close();
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
The above code block doesn't work.
Although I have passed the password via BufferedWriter, I still need to input the password manually when I run the java program.
For the security reason, I can't use the parameter "-p" to pass the password to the 7z when create the sub-process(If the process "7z" hang, the customer can see the password from the process information via the command "ps aux | grep 7z").
Can you pls figure out the issue in my code? My java version is 1.8, the OS is Ubuntu 16.04.
Appreciate.
You can't type to some command line programs using std.out, because they don't read std.out, they just read signs entered on keyboard. I belive this is the case when you enter password for 7z using command line. In order to be able to enter this password, you need to start a console window, and then mimic the behavior of keyboard, so it will be like someone is really typing into the window. You can use a java Robot class to do that. I have an Keyborad class that encloses the Robot, to simplify the proces of typing. Beware, that the in the Keyboard class on your systems some of the special characters may need to be typed differently, so you need to adjust this. Here is some pseudo code (it is for windows, but it should be easy to convert it for linux) for your case and the Keyboard.java implementation:
public static void run7z() throws IOException, InterruptedException {
startCommandLineShell();
waitABit();
keyboard.type("7z.exe e bootstrap.7z -so bootstrap.txt").enter();
waitABit();
keyboard.type("yourSecretPasswordHere").enter();
}
private static void startCommandLineShell() throws IOException {
Runtime.getRuntime().exec(new String[] { "C:\\Windows\\system32\\cmd.exe", "/C", "start" });
}
And the keyboard class:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class Keyboard {
private Robot robot;
public static void main(String... args) throws Exception
{
Keyboard keyboard = new Keyboard();
keyboard.type("~!##$%^&*()_+");
}
public Keyboard()
{
try {
this.robot = new Robot();
robot.delay(500);
robot.setAutoDelay(2);
robot.setAutoWaitForIdle(true);
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
public Keyboard(Robot robot)
{
this.robot = robot;
}
public Keyboard type(CharSequence characters)
{
int length = characters.length();
for (int i = 0; i < length; i++)
{
char character = characters.charAt(i);
type(character);
}
return this;
}
public void enter()
{
robot.keyPress(KeyEvent.VK_ENTER);
}
public void type(char character)
{
try {
switch (character) {
case 'a': doType(KeyEvent.VK_A); break;
case 'b': doType(KeyEvent.VK_B); break;
case 'c': doType(KeyEvent.VK_C); break;
case 'd': doType(KeyEvent.VK_D); break;
case 'e': doType(KeyEvent.VK_E); break;
case 'f': doType(KeyEvent.VK_F); break;
case 'g': doType(KeyEvent.VK_G); break;
case 'h': doType(KeyEvent.VK_H); break;
case 'i': doType(KeyEvent.VK_I); break;
case 'j': doType(KeyEvent.VK_J); break;
case 'k': doType(KeyEvent.VK_K); break;
case 'l': doType(KeyEvent.VK_L); break;
case 'm': doType(KeyEvent.VK_M); break;
case 'n': doType(KeyEvent.VK_N); break;
case 'o': doType(KeyEvent.VK_O); break;
case 'p': doType(KeyEvent.VK_P); break;
case 'q': doType(KeyEvent.VK_Q); break;
case 'r': doType(KeyEvent.VK_R); break;
case 's': doType(KeyEvent.VK_S); break;
case 't': doType(KeyEvent.VK_T); break;
case 'u': doType(KeyEvent.VK_U); break;
case 'v': doType(KeyEvent.VK_V); break;
case 'w': doType(KeyEvent.VK_W); break;
case 'x': doType(KeyEvent.VK_X); break;
case 'y': doType(KeyEvent.VK_Y); break;
case 'z': doType(KeyEvent.VK_Z); break;
case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break;
case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break;
case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break;
case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break;
case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break;
case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break;
case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break;
case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break;
case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break;
case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break;
case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break;
case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break;
case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break;
case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break;
case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break;
case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break;
case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break;
case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break;
case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break;
case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break;
case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break;
case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break;
case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break;
case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break;
case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break;
case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break;
case '`': doType(KeyEvent.VK_BACK_QUOTE); break;
case '0': doType(KeyEvent.VK_0); break;
case '1': doType(KeyEvent.VK_1); break;
case '2': doType(KeyEvent.VK_2); break;
case '3': doType(KeyEvent.VK_3); break;
case '4': doType(KeyEvent.VK_4); break;
case '5': doType(KeyEvent.VK_5); break;
case '6': doType(KeyEvent.VK_6); break;
case '7': doType(KeyEvent.VK_7); break;
case '8': doType(KeyEvent.VK_8); break;
case '9': doType(KeyEvent.VK_9); break;
case '-': doType(KeyEvent.VK_MINUS); break;
case '=': doType(KeyEvent.VK_EQUALS); break;
case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break;
case '!': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_1); break;//doType(KeyEvent.VK_EXCLAMATION_MARK); break;
case '#': doType(KeyEvent.VK_AT); break;
case '#': doType(KeyEvent.VK_NUMBER_SIGN); break;
case '$': doType(KeyEvent.VK_DOLLAR); break;
case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break;
case '^': doType(KeyEvent.VK_CIRCUMFLEX); break;
case '&': doType(KeyEvent.VK_AMPERSAND); break;
case '*': doType(KeyEvent.VK_ASTERISK); break;
case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break;
case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break;
case '_': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_MINUS); break;
case '+': doType(KeyEvent.VK_PLUS); break;
case '\t': doType(KeyEvent.VK_TAB); break;
case '\n': doType(KeyEvent.VK_ENTER); break;
case '[': doType(KeyEvent.VK_OPEN_BRACKET); break;
case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break;
case '\\': doType(KeyEvent.VK_BACK_SLASH); break;
case '{': doType(KeyEvent.VK_SHIFT, KeyEvent. VK_OPEN_BRACKET); break;
case '}': doType(KeyEvent.VK_SHIFT, KeyEvent. VK_CLOSE_BRACKET); break;
case '|': doType(KeyEvent.VK_SHIFT, KeyEvent. VK_BACK_SLASH); break;
case ';': doType(KeyEvent.VK_SEMICOLON); break;
case ':': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SEMICOLON); break;
case '\'': doType(KeyEvent.VK_QUOTE); break;
case '"': doType(KeyEvent.VK_QUOTEDBL); break;
case ',': doType(KeyEvent.VK_COMMA); break;
case '<': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA); break;//doType(KeyEvent.VK_LESS); break;
case '.': doType(KeyEvent.VK_PERIOD); break;
case '>': doType(KeyEvent.VK_SHIFT,KeyEvent.VK_PERIOD); break;
case '/': doType(KeyEvent.VK_SLASH); break;
case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break;
case ' ': doType(KeyEvent.VK_SPACE); break;
// default: doType(KeyEvent.getExtendedKeyCodeForChar(character));
// throw new IllegalArgumentException("Cannot type character " + character);
}
} catch (Exception e) {
System.out.println(e+" Can't type characer: "+character);
}
}
private void doType(int... keyCodes)
{
doType(keyCodes, 0, keyCodes.length);
}
private void doType(int[] keyCodes, int offset, int length)
{
if (length == 0)
return;
robot.keyPress(keyCodes[offset]);
doType(keyCodes, offset + 1, length - 1);
robot.keyRelease(keyCodes[offset]);
}
}
I am making some kind of bot with selenium. There is a point where i want to simulate random KeyPress and KeyRelease with robot class. It would be a random of String i declared. The reason why i want this is because i select a dropdown menu and then i want to select randomly an option every time bot starts. Here is how far i came:
Random r = new Random();
String alphabet = "abcdfhijlnpsu".toUpperCase();
Robot robot = new Robot();
robot.keyPress(KeyEvent.HERE I WANT TO SIMULATE RANDOM KEY PRESS FROM STRING alphabet);
robot.keyRelease(KeyEvent.HERE I WANT TO RELEASE A PREVIOUSLY PRESSED BUTTON THAT WAS RANDOMLY CHOOSEN);
String s = "foobar42"; //Target String
int l = (int)(Math.random()*s.length());
char ch = s.charAt(l); //ch will be a random character from given String
Above line could be replaced by given line
int l = new Random().nextInt(s.length());
Updated : You can use swith case to accomplish the task
switch (ch) {
case 'a': doType(VK_A); break;
case 'b': doType(VK_B); break;
case 'c': doType(VK_C); break;
case 'd': doType(VK_D); break;
case 'e': doType(VK_E); break;
case 'f': doType(VK_F); break;
case 'g': doType(VK_G); break;
case 'h': doType(VK_H); break;
case 'i': doType(VK_I); break;
case 'j': doType(VK_J); break;
case 'k': doType(VK_K); break;
case 'l': doType(VK_L); break;
case 'm': doType(VK_M); break;
case 'n': doType(VK_N); break;
case 'o': doType(VK_O); break;
case 'p': doType(VK_P); break;
case 'q': doType(VK_Q); break;
case 'r': doType(VK_R); break;
case 's': doType(VK_S); break;
case 't': doType(VK_T); break;
case 'u': doType(VK_U); break;
case 'v': doType(VK_V); break;
case 'w': doType(VK_W); break;
case 'x': doType(VK_X); break;
case 'y': doType(VK_Y); break;
case 'z': doType(VK_Z); break;
case 'A': doType(VK_SHIFT, VK_A); break;
case 'B': doType(VK_SHIFT, VK_B); break;
case 'C': doType(VK_SHIFT, VK_C); break;
case 'D': doType(VK_SHIFT, VK_D); break;
case 'E': doType(VK_SHIFT, VK_E); break;
case 'F': doType(VK_SHIFT, VK_F); break;
case 'G': doType(VK_SHIFT, VK_G); break;
case 'H': doType(VK_SHIFT, VK_H); break;
case 'I': doType(VK_SHIFT, VK_I); break;
case 'J': doType(VK_SHIFT, VK_J); break;
case 'K': doType(VK_SHIFT, VK_K); break;
case 'L': doType(VK_SHIFT, VK_L); break;
case 'M': doType(VK_SHIFT, VK_M); break;
case 'N': doType(VK_SHIFT, VK_N); break;
case 'O': doType(VK_SHIFT, VK_O); break;
case 'P': doType(VK_SHIFT, VK_P); break;
case 'Q': doType(VK_SHIFT, VK_Q); break;
case 'R': doType(VK_SHIFT, VK_R); break;
case 'S': doType(VK_SHIFT, VK_S); break;
case 'T': doType(VK_SHIFT, VK_T); break;
case 'U': doType(VK_SHIFT, VK_U); break;
case 'V': doType(VK_SHIFT, VK_V); break;
case 'W': doType(VK_SHIFT, VK_W); break;
case 'X': doType(VK_SHIFT, VK_X); break;
case 'Y': doType(VK_SHIFT, VK_Y); break;
case 'Z': doType(VK_SHIFT, VK_Z); break;
case '`': doType(VK_BACK_QUOTE); break;
case '0': doType(VK_0); break;
case '1': doType(VK_1); break;
case '2': doType(VK_2); break;
case '3': doType(VK_3); break;
case '4': doType(VK_4); break;
case '5': doType(VK_5); break;
case '6': doType(VK_6); break;
case '7': doType(VK_7); break;
case '8': doType(VK_8); break;
case '9': doType(VK_9); break;
case '-': doType(VK_MINUS); break;
case '=': doType(VK_EQUALS); break;
case '~': doType(VK_SHIFT, VK_BACK_QUOTE); break;
case '!': doType(VK_EXCLAMATION_MARK); break;
case '#': doType(VK_AT); break;
case '#': doType(VK_NUMBER_SIGN); break;
case '$': doType(VK_DOLLAR); break;
case '%': doType(VK_SHIFT, VK_5); break;
case '^': doType(VK_CIRCUMFLEX); break;
case '&': doType(VK_AMPERSAND); break;
case '*': doType(VK_ASTERISK); break;
case '(': doType(VK_LEFT_PARENTHESIS); break;
case ')': doType(VK_RIGHT_PARENTHESIS); break;
case '_': doType(VK_UNDERSCORE); break;
case '+': doType(VK_PLUS); break;
case '\t': doType(VK_TAB); break;
case '\n': doType(VK_ENTER); break;
case '[': doType(VK_OPEN_BRACKET); break;
case ']': doType(VK_CLOSE_BRACKET); break;
case '\\': doType(VK_BACK_SLASH); break;
case '{': doType(VK_SHIFT, VK_OPEN_BRACKET); break;
case '}': doType(VK_SHIFT, VK_CLOSE_BRACKET); break;
case '|': doType(VK_SHIFT, VK_BACK_SLASH); break;
case ';': doType(VK_SEMICOLON); break;
case ':': doType(VK_COLON); break;
case '\'': doType(VK_QUOTE); break;
case '"': doType(VK_QUOTEDBL); break;
case ',': doType(VK_COMMA); break;
case '<': doType(VK_SHIFT, VK_COMMA); break;
case '.': doType(VK_PERIOD); break;
case '>': doType(VK_SHIFT, VK_PERIOD); break;
case '/': doType(VK_SLASH); break;
case '?': doType(VK_SHIFT, VK_SLASH); break;
case ' ': doType(VK_SPACE); break;
default:
throw new IllegalArgumentException("Cannot type character " + character);
OR
String code = "VK_" + ch
Field f = KeyEvent.class.getField(code);
int keyEvent = f.getInt(null);
robot.press(keyEvent);
robot.release(keyEvent);
Use the following way to do the same
Random rand = new Random();
char randomchar = (char) ('A' + Math.random() * ('Z'-'A' + 1));
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_+randomchar);
robot.keyRelease(KeyEvent.VK_+randomchar);
Updated :
String allchars = "abcdfhijlnpsu";
char letter = allchars.charAt(rand.nextInt(allchars.length()));
I'm making a caesar cipher program in java. I used the switch statement and I only have one problem is when I write my full name there will be no space between my first and last.
import java.util.*;
public class caesarCipher {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Write your name:");
String name = in.nextLine();
System.out.println("The Encryption for your name:");
for (int i = 0; i < name.length(); i++) {
switch (name.charAt(i)) {
case 'A':
case 'a':
System.out.print("E");
break;
case 'B':
case 'b':
System.out.print("Y");
break;
case 'C':
case 'c':
System.out.print("F");
break;
case 'D':
case 'd':
System.out.print("Q");
break;
case 'E':
case 'e':
System.out.print("W");
break;
case 'F':
case 'f':
System.out.print("D");
break;
case 'G':
case 'g':
System.out.print("T");
break;
case 'H':
case 'h':
System.out.print("C");
break;
case 'I':
case 'i':
System.out.print("R");
break;
case 'J':
case 'j':
System.out.print("N");
break;
case 'K':
case 'k':
System.out.print("B");
break;
case 'L':
case 'l':
System.out.print("G");
break;
case 'M':
case 'm':
System.out.print("A");
break;
case 'N':
case 'n':
System.out.print("J");
break;
case 'O':
case 'o':
System.out.print("X");
break;
case 'P':
case 'p':
System.out.print("O");
break;
case 'Q':
case 'q':
System.out.print("I");
break;
case 'R':
case 'r':
System.out.print("L");
break;
case 'S':
case 's':
System.out.print("Z");
break;
case 'T':
case 't':
System.out.print("M");
break;
case 'U':
case 'u':
System.out.print("P");
break;
case 'V':
case 'v':
System.out.print("S");
break;
case 'W':
case 'w':
System.out.print("H");
break;
case 'X':
case 'x':
System.out.print("K");
break;
case 'Y':
case 'y':
System.out.print("V");
break;
case 'Z':
case 'z':
System.out.print("U");
break;
}
}
}
Since you have a case for every letter but not for a space, it will just get dropped during your encryption process. You could add a default at the end of the switch statement that just outputs the letter without encrypting it. This would then catch the space as well.
// ...
case 'Z':
case 'z':
System.out.print("U");
break;
default:
System.out.print(name.charAt(i));
break;
You don't want to use a default case to handle a space, because then all other symbols you haven't handled would be represented by a space.
Simply continue your same method of handling characters:
case ' ':
System.out.print(" ");
break;
Modify the program to accept a telephone number with any number of letters. The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits. Also, modify the program to process as many telephone numbers as the user wants.
Below is what i have to edit.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
}
This part is what i tried.
Scanner keyboard = new Scanner(System.in);
String telephone_letter;
int index;
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
char aChar[] = telephone_letter.toCharArray();
int[] number = new int[100];
while(telephone_letter !="2")
{
for(index=0;index<telephone_letter.length();index++){
switch (aChar[index])
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
number[index] = 2;
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
number[index] = 3;
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
number[index] = 4;
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
number[index] = 5;
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
number[index] = 6;
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
number[index] = 7;
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
number[index] = 8;
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
number[index] = 9;
break;
default:
break;
}
}
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + telephone_letter);
System.out.println("The Phone number is : " + number[0]+number[1]+number[2]+"-"+number[3]+number[4]+number[5]+number[6]);
System.out.println("=======================================");
System.out.print("Enter Telephone letters : ");
telephone_letter = keyboard.nextLine();
}
My output is wrong.
Enter Telephone letters : fewfwef
======================================
The Telephone letter is : fewfwef
The Phone number is : 339-3933
=======================================
Enter Telephone letters : wsqsq
=======================================
The Telephone letter is : wsqsq
The Phone number is : 339-3933
=======================================
And another Question is how do i do this The output should display a hyphen after the first 3 digits and subsequently a hyphen (-) after every four digits.
This seems like a homework problem so here are some hints to help you solve it yourself.
(1) Using a fixed number like 100 is rarely a good idea:
int[] number = new int[aChar.length];
(2) The scanner can tell you if there is more to be scanned(read):
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
while (keyboard.hasNextLine()) {
String telephone_letter = keyboard.nextLine();
...
System.out.println("");
System.out.print("Enter Telephone letters : ");
}
(3) A loop is good for counting. If the first count is different, then start different:
int todash = 3;
System.out.print("The Phone number is : ");
for (int i=0; i < number.length; ++i) {
if (todash == 0) {
System.out.print("-");
todash = 4;
}
System.out.print(number[i]);
--todash;
}
System.out.println("");
I've rewritten you're code. It may be easier to work with.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Telephone {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Telephone letters : ");
String input = keyboard.nextLine();
TelephoneProcessor telephoneProcessor = new TelephoneProcessor(input);
String result = telephoneProcessor.proccess();
System.out.println("=======================================");
System.out.println("The Telephone letter is : " + input);
System.out.println("The Phone number is : " + result);
System.out.println("=======================================");
}
}
class TelephoneProcessor {
Map<String, String> matches = new HashMap<>();
String toProcess;
public TelephoneProcessor(String toProcess) {
this.toProcess = toProcess;
matches.put("[a-c]", "2");
matches.put("[d-f]", "3");
matches.put("[g-i]", "4");
matches.put("[j-l]", "5");
matches.put("[m-o]", "6");
matches.put("[p-r]", "7");
matches.put("[s-w]", "8");
matches.put("[x-z]", "9");
}
public String proccess() {
if(toProcess != "2") {
matches.forEach((regex, replacement) -> {
toProcess = toProcess.replaceAll(regex, replacement);
});
}
return toProcess.substring(0,3) + "-" + toProcess.substring(3,7);
}
}
It only asks once but that's easy to solve by making a class that contains the main method in a method and loops it.
I hope this helps for further extensions of the program :)