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;
Related
I am writing a program that simulates the translation of an alphabetic phone number into just numbers. For example: 888-get-food == 555-438-3663.
Initially, the user should enter the phone number on the following format: 888-GET-FOOD (With the dashes). When I try to check if there are dashes on the user input, it prints the dashes, but with the number 1 in front of it. Very annoying.
This is what I have so far:
// Ask the user to enter the phone number
System.out.println("Please enter the phone number: ");
// Save the phone number into a string
String initialPhoneNumber = input.nextLine();
// Convert user input to UPPERCASE
initialPhoneNumber = initialPhoneNumber.toUpperCase();
// This will be the phone number converted
String finalPhoneNumber = fullPhoneNumber(initialPhoneNumber);
// Print number
System.out.println(initialPhoneNumber);
System.out.println(finalPhoneNumber);
for (int i = 0; i < strLength; i++) {
char letter = initialPhoneNumber.charAt(i);
if (Character.isLetter(letter)) {
switch (letter) {
case 'A': case 'B': case 'C': number = 2; break;
case 'D': case 'E': case 'F': number = 3; break;
case 'G': case 'H': case 'I': number = 4; break;
case 'J': case 'K': case 'L': number = 5; break;
case 'M': case 'N': case 'O': number = 6; break;
case 'P': case 'Q': case 'R': case 'S': number = 7; break;
case 'T': case 'U': case 'V': number = 8; break;
case 'W': case 'X': case 'Y': case 'Z': number = 9; break;
case '-':number='-';
}
}
else if (Character.isDigit(letter)) {
number = Character.getNumericValue(letter);
}
else if (initialPhoneNumber.charAt(i) == '-') {
number = Character.getNumericValue(letter);
}
and this is the output:
Please enter the phone number:
555-GET-FOOD
555-GET-FOOD
555-1438-13663
Why does the number 1 show up in front of the dashes? How can I make it so it doesn't show up? In other words, how can I print or separate the numbers separated by dashes?
Thanks
The problem is within the statement:
else if (initialPhoneNumber.charAt(i) == '-') {
number = Character.getNumericValue(letter);
}
The method Character.getNumericValue() returrns -1 if the character passed as parameter is not numeric.
Since you're passing -, the method returns -1.
That's why you get the 1 after the dash.
UPDATE
Supposing that the for-loop is part of your method fullPhoneNumber, you could resolve it as follows:
public String fullPhoneNumber(String initialPhoneNumber)
{
StringBuilder result;
result = new StringBuilder();
for (int i = 0; i < initialPhoneNumber.length(); i++)
{
char letter = Character.toUpperCase(initialPhoneNumber.charAt(i));
switch (letter)
{
case 'A':
case 'B':
case 'C':
letter = '2';
break;
case 'D':
case 'E':
case 'F':
letter = '3';
break;
case 'G':
case 'H':
case 'I':
letter = '4';
break;
case 'J':
case 'K':
case 'L':
letter = '5';
break;
case 'M':
case 'N':
case 'O':
letter = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
letter = '7';
break;
case 'T':
case 'U':
case 'V':
letter = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
letter = '9';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
break;
default:
throw new IllegalArgumentException("");
}
result.append(letter);
} // for
return (result.toString());
} // fullPhoneNumber
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 working on a system to type things automatically with java. This is how I write it:
public void typeMessage(String message) {
for (char c : message.toCharArray()) {
int code = c;
if (code > 96 && code < 123)
code = code - 32;
if (c == '#') {
robot.keyPress(VK_SHIFT);
robot.keyPress(VK_AT);
robot.keyRelease(VK_SHIFT);
robot.keyRelease(VK_AT);
} else {
type(code);
}
}
type(VK_ENTER);
}
But I'm getting this error:
Exception in thread "Thread-2" java.lang.IllegalArgumentException: Invalid key code
on
robot.keyPress(VK_AT);
The Class didnt reach all ascii characters, but can resolve your problem, costumize it.
KeyboardKeys kk = new KeyboardKeys();
kk.keyPress('#');
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent.*;
import static java.awt.event.KeyEvent.*;
public class KeyboardKeys {
private Robot robot;
public KeyboardKeys() throws AWTException {
this.robot = new Robot();
}
public KeyboardKeys(Robot robot) {
this.robot = robot;
}
public void keyPress(char characterKey){
switch (characterKey){
case '☺': altNumpad("1"); break;
case '☻': altNumpad("2"); break;
case '♥': altNumpad("3"); break;
case '♦': altNumpad("4"); break;
case '♣': altNumpad("5"); break;
case '♠': altNumpad("6"); break;
case '♂': altNumpad("11"); break;
case '♀': altNumpad("12"); break;
case '♫': altNumpad("14"); break;
case '☼': altNumpad("15"); break;
case '►': altNumpad("16"); break;
case '◄': altNumpad("17"); break;
case '↕': altNumpad("18"); break;
case '‼': altNumpad("19"); break;
case '¶': altNumpad("20"); break;
case '§': altNumpad("21"); break;
case '▬': altNumpad("22"); break;
case '↨': altNumpad("23"); break;
case '↑': altNumpad("24"); break;
case '↓': altNumpad("25"); break;
case '→': altNumpad("26"); break;
case '←': altNumpad("27"); break;
case '∟': altNumpad("28"); break;
case '↔': altNumpad("29"); break;
case '▲': altNumpad("30"); break;
case '▼': altNumpad("31"); break;
case '!': altNumpad("33"); break;
case '"': altNumpad("34"); break;
case '#': altNumpad("35"); break;
case '$': altNumpad("36"); break;
case '%': altNumpad("37"); break;
case '&': altNumpad("38"); break;
case '\'': altNumpad("39"); break;
case '(': altNumpad("40"); break;
case ')': altNumpad("41"); break;
case '*': altNumpad("42"); break;
case '+': altNumpad("43"); break;
case ',': altNumpad("44"); break;
case '-': altNumpad("45"); break;
case '.': altNumpad("46"); break;
case '/': altNumpad("47"); break;
case '0': altNumpad("48"); break;
case '1': altNumpad("49"); break;
case '2': altNumpad("50"); break;
case '3': altNumpad("51"); break;
case '4': altNumpad("52"); break;
case '5': altNumpad("53"); break;
case '6': altNumpad("54"); break;
case '7': altNumpad("55"); break;
case '8': altNumpad("56"); break;
case '9': altNumpad("57"); break;
case ':': altNumpad("58"); break;
case ';': altNumpad("59"); break;
case '<': altNumpad("60"); break;
case '=': altNumpad("61"); break;
case '>': altNumpad("62"); break;
case '?': altNumpad("63"); break;
case '#': altNumpad("64"); break;
case 'A': altNumpad("65"); break;
case 'B': altNumpad("66"); break;
case 'C': altNumpad("67"); break;
case 'D': altNumpad("68"); break;
case 'E': altNumpad("69"); break;
case 'F': altNumpad("70"); break;
case 'G': altNumpad("71"); break;
case 'H': altNumpad("72"); break;
case 'I': altNumpad("73"); break;
case 'J': altNumpad("74"); break;
case 'K': altNumpad("75"); break;
case 'L': altNumpad("76"); break;
case 'M': altNumpad("77"); break;
case 'N': altNumpad("78"); break;
case 'O': altNumpad("79"); break;
case 'P': altNumpad("80"); break;
case 'Q': altNumpad("81"); break;
case 'R': altNumpad("82"); break;
case 'S': altNumpad("83"); break;
case 'T': altNumpad("84"); break;
case 'U': altNumpad("85"); break;
case 'V': altNumpad("86"); break;
case 'W': altNumpad("87"); break;
case 'X': altNumpad("88"); break;
case 'Y': altNumpad("89"); break;
case 'Z': altNumpad("90"); break;
case '[': altNumpad("91"); break;
case '\\': altNumpad("92"); break;
case ']': altNumpad("93"); break;
case '^': altNumpad("94"); break;
case '_': altNumpad("95"); break;
case '`': altNumpad("96"); break;
case 'a': altNumpad("97"); break;
case 'b': altNumpad("98"); break;
case 'c': altNumpad("99"); break;
case 'd': altNumpad("100"); break;
case 'e': altNumpad("101"); break;
case 'f': altNumpad("102"); break;
case 'g': altNumpad("103"); break;
case 'h': altNumpad("104"); break;
case 'i': altNumpad("105"); break;
case 'j': altNumpad("106"); break;
case 'k': altNumpad("107"); break;
case 'l': altNumpad("108"); break;
case 'm': altNumpad("109"); break;
case 'n': altNumpad("110"); break;
case 'o': altNumpad("111"); break;
case 'p': altNumpad("112"); break;
case 'q': altNumpad("113"); break;
case 'r': altNumpad("114"); break;
case 's': altNumpad("115"); break;
case 't': altNumpad("116"); break;
case 'u': altNumpad("117"); break;
case 'v': altNumpad("118"); break;
case 'w': altNumpad("119"); break;
case 'x': altNumpad("120"); break;
case 'y': altNumpad("121"); break;
case 'z': altNumpad("122"); break;
case '{': altNumpad("123"); break;
case '|': altNumpad("124"); break;
case '}': altNumpad("125"); break;
case '~': altNumpad("126"); break;
case '⌂': altNumpad("127"); break;
case 'Ç': altNumpad("128"); break;
case 'ü': altNumpad("129"); break;
case 'é': altNumpad("130"); break;
case 'â': altNumpad("131"); break;
case 'ä': altNumpad("132"); break;
case 'à': altNumpad("133"); break;
case 'å': altNumpad("134"); break;
case 'ç': altNumpad("135"); break;
case 'ê': altNumpad("136"); break;
case 'ë': altNumpad("137"); break;
case 'è': altNumpad("138"); break;
case 'ï': altNumpad("139"); break;
case 'î': altNumpad("140"); break;
case 'ì': altNumpad("141"); break;
case 'Ä': altNumpad("142"); break;
case 'Å': altNumpad("143"); break;
case 'É': altNumpad("144"); break;
case 'æ': altNumpad("145"); break;
case 'Æ': altNumpad("146"); break;
case 'ô': altNumpad("147"); break;
case 'ö': altNumpad("148"); break;
case 'ò': altNumpad("149"); break;
case 'û': altNumpad("150"); break;
case 'ù': altNumpad("151"); break;
case 'ÿ': altNumpad("152"); break;
case 'Ö': altNumpad("153"); break;
case 'Ü': altNumpad("154"); break;
case '¢': altNumpad("155"); break;
case '£': altNumpad("156"); break;
case '¥': altNumpad("157"); break;
case '₧': altNumpad("158"); break;
case 'ƒ': altNumpad("159"); break;
case 'á': altNumpad("160"); break;
case 'í': altNumpad("161"); break;
case 'ó': altNumpad("162"); break;
case 'ú': altNumpad("163"); break;
case 'ñ': altNumpad("164"); break;
case 'Ñ': altNumpad("165"); break;
case 'ª': altNumpad("166"); break;
case 'º': altNumpad("167"); break;
case '¿': altNumpad("168"); break;
case '⌐': altNumpad("169"); break;
case '¬': altNumpad("170"); break;
case '½': altNumpad("171"); break;
case '¼': altNumpad("172"); break;
case '¡': altNumpad("173"); break;
case '«': altNumpad("174"); break;
case '»': altNumpad("175"); break;
case '░': altNumpad("176"); break;
case '▒': altNumpad("177"); break;
case '▓': altNumpad("178"); break;
case '│': altNumpad("179"); break;
case '┤': altNumpad("180"); break;
case '╡': altNumpad("181"); break;
case '╢': altNumpad("182"); break;
case '╖': altNumpad("183"); break;
case '╕': altNumpad("184"); break;
case '╣': altNumpad("185"); break;
case '║': altNumpad("186"); break;
case '╗': altNumpad("187"); break;
case '╝': altNumpad("188"); break;
case '╜': altNumpad("189"); break;
case '╛': altNumpad("190"); break;
case '┐': altNumpad("191"); break;
case '└': altNumpad("192"); break;
case '┴': altNumpad("193"); break;
case '┬': altNumpad("194"); break;
case '├': altNumpad("195"); break;
case '─': altNumpad("196"); break;
case '┼': altNumpad("197"); break;
case '╞': altNumpad("198"); break;
case '╟': altNumpad("199"); break;
case '╚': altNumpad("200"); break;
case '╔': altNumpad("201"); break;
case '╩': altNumpad("202"); break;
case '╦': altNumpad("203"); break;
case '╠': altNumpad("204"); break;
case '═': altNumpad("205"); break;
case '╬': altNumpad("206"); break;
case '╧': altNumpad("207"); break;
case '╨': altNumpad("208"); break;
case '╤': altNumpad("209"); break;
case '╥': altNumpad("210"); break;
case '╙': altNumpad("211"); break;
case '╘': altNumpad("212"); break;
case '╒': altNumpad("213"); break;
case '╓': altNumpad("214"); break;
case '╫': altNumpad("215"); break;
case '╪': altNumpad("216"); break;
case '┘': altNumpad("217"); break;
case '┌': altNumpad("218"); break;
case '█': altNumpad("219"); break;
case '▄': altNumpad("220"); break;
case '▌': altNumpad("221"); break;
case '▐': altNumpad("222"); break;
case '▀': altNumpad("223"); break;
case 'α': altNumpad("224"); break;
case 'ß': altNumpad("225"); break;
case 'Γ': altNumpad("226"); break;
case 'π': altNumpad("227"); break;
case 'Σ': altNumpad("228"); break;
case 'σ': altNumpad("229"); break;
case 'µ': altNumpad("230"); break;
case 'τ': altNumpad("231"); break;
case 'Φ': altNumpad("232"); break;
case 'Θ': altNumpad("233"); break;
case 'Ω': altNumpad("234"); break;
case 'δ': altNumpad("235"); break;
case '∞': altNumpad("236"); break;
case 'φ': altNumpad("237"); break;
case 'ε': altNumpad("238"); break;
case '∩': altNumpad("239"); break;
case '≡': altNumpad("240"); break;
case '±': altNumpad("241"); break;
case '≥': altNumpad("242"); break;
case '≤': altNumpad("243"); break;
case '⌠': altNumpad("244"); break;
case '⌡': altNumpad("245"); break;
case '÷': altNumpad("246"); break;
case '≈': altNumpad("247"); break;
case '°': altNumpad("248"); break;
case '∙': altNumpad("249"); break;
case '·': altNumpad("250"); break;
case '√': altNumpad("251"); break;
case 'ⁿ': altNumpad("252"); break;
case '²': altNumpad("253"); break;
case '■': altNumpad("254"); break;
default: return;
}
}
private void altNumpad(int... numpadCodes){
if (numpadCodes.length == 0) {
return;
}
robot.keyPress(VK_ALT);
for (int NUMPAD_KEY : numpadCodes){
robot.keyPress(NUMPAD_KEY);
robot.keyRelease(NUMPAD_KEY);
}
robot.keyRelease(VK_ALT);
}
private void altNumpad(String numpadCodes){
if (numpadCodes == null || !numpadCodes.matches("^\\d+$")){
return;
}
robot.keyPress(VK_ALT);
for (char charater : numpadCodes.toCharArray()){
int NUMPAD_KEY = getNumpad(charater);
if (NUMPAD_KEY != -1){
robot.keyPress(NUMPAD_KEY);
robot.keyRelease(NUMPAD_KEY);
}
}
robot.keyRelease(VK_ALT);
}
private int getNumpad(char numberChar){
switch (numberChar){
case '0' : return VK_NUMPAD0;
case '1' : return VK_NUMPAD1;
case '2' : return VK_NUMPAD2;
case '3' : return VK_NUMPAD3;
case '4' : return VK_NUMPAD4;
case '5' : return VK_NUMPAD5;
case '6' : return VK_NUMPAD6;
case '7' : return VK_NUMPAD7;
case '8' : return VK_NUMPAD8;
case '9' : return VK_NUMPAD9;
default: return -1;
}
}
}
Your keyboard layout should have a key for the # symbol for this code to work. Does it?
If your keyboard is set up with an American layout you need to type shift+2 to type an # symbol, and instead of VK_AT you have to use VK_2.
not sure whether this only creating the problem, but you should consider releasing other key before releasing shift.
robot.keyRelease(VK_AT);
robot.keyRelease(VK_SHIFT);