Input Repeat with same case - java

After I run this codes, it says BUILD SUCCESSFUL and it stops. What should I do to make it repeat to ask another input with same case? I did read tutorial on several web but I'm not helped by those.
Here's the code:
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
I appreciate any help that you can provide.

I would try using a while loop to encase the switch. So it keeps asking your input.
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
while(true){
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
}

As suggested by MadProgarmmer do-while loop is the perfect use-case for such scenarios. You need to wrap your input line plus switch cases and print statement inside the do-while loop as follow:
do {
//so you know that terminal is asking for input
System.out.println("Input: ");
mth =scanner.nextInt();
//copy/paste your switch case with last System.out.println() here
} while (true);
I am worried about having execution that never ends. Maybe you want to end the loop when the user supplies -1, so change the while(true) to while (mth != -1) and that should be your termination input.

Related

Menu loop in 2 different methods Java program

I am working on a menu program and I want to be able to enter as many selections as I want without the menu looping as well after the input, currently it either infinite-loops, or the program ends after one input, being unable to perform a different selection after the first. Also I want to have a case where I exit the menu if I press 0.
public void showMenu() {
System.out.println("Welcome!");
System.out.println("Select an option:\n" +
"1. Adunare\n" +
"2. Scadere\n" +
"3. Inmultire\n" +
"4. Impartire\n" +
"5. Comparare numere\n" +
"6. List To Hundred\n" +
"7. Nr to list\n" +
"8. Contains\n" +
"9. Even numbers\n" +
"10. List of Strings\n" +
"11. Second largest number\n" +
"12. Second lowest number\n" +
"13. Number \n" +
"14. Number 2 \n" +
"15. Number 3\n" +
"16. String\n" +
"17. String 2\n" +
"18. Amount of snow\n" +
"19. Eligible to vote test\n" +
"20. Odd or even\n" +
"21. Dog\n" +
"22. Cat\n" +
"23. Elev");
}
public void runProgram() {
showMenu();
int numberFromUser = citire.readNumbers();
do {
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Add
numberFromUser = citire.readNumbers();
into the loop to take numbers in the loop.
public void runProgram() {
showMenu();
int numberFromUser;
do {
numberFromUser = citire.readNumbers();
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Try changing runProgram to following :
public void runProgram()
{
int numberFromUser;
showMenu();
while(true)
{
System.out.print("Enter choice: ");
numberFromUser = citire.readNumbers();
switch(NumberFromUser)
{
// All your case statements
case 0: System.exit(0);
}
}
}

How to decomrepss a password protected 7zip archive with Java on Linux?

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]);
}
}

How can i simulate random robot keypress in Java?

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()));

How to make one class into two classes

I finished a program which is to make a number between 1 to 3999 and change it to roman numerals and I got it to work but I have to put this in two classes a main class and a tester class how would I do this? I know this may be a really simple question but I can't seem to figure it out how to split them into a main class and a tester class.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
String numberString="";
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
System.out.println(number + " in Roman numerals is " + numberString);
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
As #Vikrant Kashyap pointed out. You can break this down to the test class and the conversion class. I didn't have a chance to compile the code. Let me know if this works or not.
RomanNumeralsTest.java
public class RomanNumeralsTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
RomanNumerals rn = new RomanNumerals();
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
System.out.println(number + " in Roman numerals is " + rn.convertToRomanNumeral(number));
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
}
RomanNumerals.java
public class RomanNumerals
{
public String convertToRomanNumeral(int number)
{
String numberString = "";
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
return numberString;
}
}

Invalid key code # java

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);

Categories

Resources