Take character and return the Keypad equivalant - java

So I am writing code that will take a character and return its alpha-numeric keypad equivalent as a character.
The problem is, I am getting a question mark in a box as a return. I have checked that the input is correct. For example, with a char 'h' input, i should get a char '4' return. Hoping someone is able to spot my error.
Code is below:
public char getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}

The return type of your method is char.
Now take your switch statement. You return char values in the range from 2 to 9. Now look at an ASCII table.
Surprise: these chars are all "unprintable" control characters. Thus your console gives you "?" when you print them!
If you want '4', your code has to return '4', not 4! Or 52, because that entry in the ASCII table represents '4'.

You are not getting proper output because you are using char as a return type of getDigit(..) function. You should use int as a return type instead of char as in switch case you are comparing with characters and returning digit values So replace your code with the following code, this will work:
public int getDigit(char letter) throws Exception{
switch (letter) {
case 'a': case 'b': case 'c': case '2':
return 2;
case 'd': case 'e': case 'f': case '3':
return 3;
case 'g': case 'h': case 'i': case '4':
return 4;
case 'j': case 'k': case 'l': case '5':
return 5;
case 'm': case 'n': case 'o': case '6':
return 6;
case 'p': case 'q': case 'r': case 's': case '7':
return 7;
case 't': case 'u': case 'v': case '8':
return 8;
case 'w': case 'x': case 'y': case 'z': case '9':
return 9;
default:
throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");
}
}

Related

How do I get my for loop to go to the next character?

I'm creating a code that will output a number based on the input letter. My code is stuck was stuck on a loop of doing character 0 repeatedly. Friend said that char Check = userInput.charAt(numTelephone); would fix it. When I placed that into my code I recieved the error: error: char cannot be dereferenced char Check = userInput.charAt(numTelephone);
import java.util.Scanner;
public class PhoneWords {
public static void main(String []args){
Scanner scnr = new Scanner(System.in);
int numTelephone;
char userInput;
userInput = scnr.next().charAt(0);
System.out.println("Enter a telephone number using letters (EXIT to quit): ");
System.out.println("The corresponding telephone number is: ");
if ((userInput >= 'A') || (userInput <= 'Z') || (userInput >= 'a') || (userInput <= 'z')){
for (numTelephone = 0; numTelephone < 7; numTelephone++){
char Check = userInput.charAt(numTelephone);
if (numTelephone == 3){
System.out.print("-");
}
switch (userInput) {
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
System.out.print("2");
break;
case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
System.out.print("3");
break;
case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
System.out.print("4");
break;
case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
System.out.print("5");
break;
case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
System.out.print("6");
break;
case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
System.out.print("7");
break;
case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
System.out.print("8");
break;
case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
System.out.print("9");
break;
default:
break;
}
}
}
}
}
The type char is primitive -- not an object -- so it cannot be dereferenced
Dereferencing is the process of accessing the value referred to by a reference. Since a char is already a value (not a reference), it can not be dereferenced.
so get the char from the given input like:
char userInput = scnr.nextLine().charAt(0);
And comment this line which is suggested by your friend which is not used any where:
//char Check = userInput.charAt(numTelephone);

How to loop a switch statement while using StringBuilder?

My current set of switch statements work, but they take up far too many lines and I am unsure of how to create a working loop to condense the switch statements.
Here is my current switch statement within a method (I have six, the following are incremented by 1)
public static String convert (String str) {
String strb = new StringBuilder(str);
switch (str.charAt(4)) {
case 'a':
case 'b':
case 'c':
strb.insert(4, 2);
strb.deleteCharAt(5);
break;
case 'd':
case 'e':
case 'f':
strb.insert(4, 3);
strb.deleteCharAt(5);
break;
case 'g':
case 'h':
case 'i':
strb.insert(4, 4);
strb.deleteCharAt(5);
break;
case 'j':
case 'k':
case 'l':
strb.insert(4, 5);
strb.deleteCharAt(5);
break;
case 'm':
case 'n':
case 'o':
strb.insert(4, 6);
strb.deleteCharAt(5);
break;
case 'p':
case 'q':
case 'r':
case 's':
strb.insert(4, 7);
strb.deleteCharAt(5);
break;
case 't':
case 'u':
case 'v':
strb.insert(4, 8);
strb.deleteCharAt(5);
break;
case 'w':
case 'x':
case 'y':
case 'z':
strb.insert(4, 9);
strb.deleteCharAt(5);
break;
}
return strb.toString();
}
I have tried a for loop, but it does not seem to work. Any suggestions?
for (index = 4; index < str.length(); index++) {
switch (str.charAt(index)) {
case 'a':
case 'b':
case 'c':
strb.insert(index, 2);
strb.deleteCharAt(index + 1);
break;
case 'd':
case 'e':
case 'f':
strb.insert(index, 3);
strb.deleteCharAt(index + 1);
break;
case 'g':
case 'h':
case 'i':
strb.insert(index, 4);
strb.deleteCharAt(index + 1);
break;
case 'j':
case 'k':
case 'l':
strb.insert(index, 5);
strb.deleteCharAt(index + 1);
break;
case 'm':
case 'n':
case 'o':
strb.insert(index, 6);
strb.deleteCharAt(index + 1);
break;
case 'p':
case 'q':
case 'r':
case 's':
strb.insert(index, 7);
strb.deleteCharAt(index + 1);
break;
case 't':
case 'u':
case 'v':
strb.insert(index, 8);
strb.deleteCharAt(index + 1);
break;
case 'w':
case 'x':
case 'y':
case 'z':
strb.insert(index, 9);
strb.deleteCharAt(index + 1);
break;
}
}
A better approach is to remove the switch statement altogether, and use a lookup table to select the digit:
private static final String DIGIT_LOOKUP = "22233344455566677778889999";
...
int pos = Character.toLowerCase(str.charAt(index)) - 'a';
char digit = DIGIT_LOOKUP.charAt(pos);
Demo.

I want to write a program to find the coordinate number for each letter on the phone pad. I wote like this

import java.util.Scanner;
public class ConvertLetterToNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter one letter");
String str = input.next();
char ch = str.charAt(0);
switch (ch){
case 'A': case 'a': case 'B': case 'b': case 'C': case 'c':
System.out.println("the number is 2");
break;
case 'D': case 'd': case 'E': case 'e': case 'F': case 'f':
System.out.println("the number is 3");
break;
case 'G': case 'g': case 'H': case 'h': case 'I': case 'i':
System.out.println("the number is 4");
break;
case 'J': case 'j': case 'K': case 'k': case 'L': case 'l':
System.out.println("the number is 5");
break;
case 'M': case 'm': case 'N': case 'n': case 'O': case 'o':
System.out.println("the number is 6");
break;
case 'P':case 'p': case 'Q': case 'q': case 'R': case 'r': case 'S': case's':
System.out.println("the number is 7");
break;
case 'T':case 't':case 'U':case'u':case'V':case 'v':
System.out.println("the number is 8");
break;
case 'W':case 'w':case 'X': case 'x': case 'Y':case'y':case 'Z':case 'z':
System.out.println("the number is 9");
break;
default :System.out.println("Error:invalid input");
System.exit(1);
}
}
}
the problem is if I enter two or more letters at one time (for example "ab" the answer will be "2", but what I want is "Invalid input". How can I add this instruction to ask user to enter only one letter?
You could check the str length before your switch, and return like so:
if (str.length > 1){
return;
}

Auto login desktop application using java

I want to autologin to any desktop application like Lotus, Outlook, or any other applications (who needs user authentication) using Java.
Let say I want to auto login to desktop Skype app using Java program i.e we will open skype.exe & will provide authentication details(username, password) as parameters.
While searching, I got to know we have a Robot API , so I was trying with it in below way.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static java.awt.event.KeyEvent.*;
public class RobotExp {
Robot robot = new Robot();
public static void main(String[] args) {
try {
// Process process = new ProcessBuilder(
// "C:\\Windows\\System32\\notepad.exe").start();//C:\SmartClientCache\Apps\Ellie
// Mae\Encompass
Process process = new ProcessBuilder(
"C:\\Program Files (x86)\\Skype\\Phone\\Skype.exe").start();
new RobotExp();
} catch (Exception e) {
// TODO: handle exception
}
}
public RobotExp() throws AWTException {
robot.delay(1200);
// robot.keyPress(KeyEvent.VK_TAB);
// robot.delay(200);
type("testaccess");
// robot.setAutoDelay(40);
// robot.setAutoWaitForIdle(true);
//
// robot.delay(4000);
// robot.mouseMove(40, 130);
// robot.delay(500);
//
robot.keyPress(KeyEvent.VK_TAB);
type("test#1234");
// robot.delay(200);
robot.keyPress(KeyEvent.VK_ENTER);
// type("wish is your");
// robot.delay(500);
// leftClick();
//
// robot.delay(500);
// type("Hello, world");
//
// robot.mouseMove(40, 160);
// robot.delay(500);
//
// leftClick();
// robot.delay(500);
// leftClick();
//
// robot.delay(500);
// type("This is a test of the Java Robot class");
//
// robot.delay(50);
// type(KeyEvent.VK_DOWN);
//
// robot.delay(250);
// type("Four score and seven years ago, our fathers ...");
//
// robot.delay(1000);
System.exit(0);
}
// private void leftClick() {
// robot.mousePress(InputEvent.BUTTON1_MASK);
// robot.delay(200);
// robot.mouseRelease(InputEvent.BUTTON1_MASK);
// robot.delay(200);
// }
// private void rightClick() {
// robot.mousePress(InputEvent.BUTTON2_MASK);
// robot.delay(200);
// robot.mouseRelease(InputEvent.BUTTON2_MASK);
// robot.delay(200);
// }
// private void type(int i) {
// robot.delay(40);
// robot.keyPress(i);
// robot.keyRelease(i);
// }
// private void type(String s) {
// byte[] bytes = s.getBytes();
// for (byte b : bytes) {
// int code = b;
// // keycode only handles [A-Z] (which is ASCII decimal [65-90])
// if (code > 96 && code < 123)
// code = code - 32;
// robot.delay(40);
// robot.keyPress(code);
// robot.keyRelease(code);
// }
// }
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_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_LESS);
break;
case '.':
doType(VK_PERIOD);
break;
case '>':
doType(VK_GREATER);
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);
}
}
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]);
}
}
but still not getting any success.
Is it really possible to make auto login to any desktop application?
Please let me know if any suggestions or ways to available to achieve it?
As far as I know this isn't possible in Java. Every aplication has it's own login mechanism and primary purpose of the Robot API is for automated testing of Java platform implementations.

Getting Wrong Count in JAVA Count Letters Program

Can anyone tell me what's wrong with my code, why am I not getting the correct letter count?
This program reads a text file and count each English letters, A-Z, and a-z, not case sensitive.
Thank you for helping.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Solution {
private static int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
public static void print(){
int[] in = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
for (int i = 0; i < in.length; i++){
System.out.println(in[i]);
}
}
public static void main(String[] args) throws FileNotFoundException{
File file = new File("t.txt");
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.toLowerCase();
for (int i = 0; i < line.length(); i++) {
switch(line.charAt(i)) {
case 'a': a++;break;
case 'b': b++;break;
case 'c': c++;break;
case 'd': d++;break;
case 'e': e++;break;
case 'f': f++;break;
case 'g': g++;break;
case 'h': h++;break;
case 'i': i++;break;
case 'j': j++;break;
case 'k': k++;break;
case 'l': l++;break;
case 'm': m++;break;
case 'n': n++;break;
case 'o': o++;break;
case 'p': p++;break;
case 'q': q++;break;
case 'r': r++;break;
case 's': s++;break;
case 't': t++;break;
case 'u': u++;break;
case 'v': v++;break;
case 'w': w++;break;
case 'x': x++;break;
case 'y': y++;break;
case 'z': z++;break;
}
}
}
print();
}
}
The problem is that when you encounter a i, that will increment the variable of the loop, not the one in the array. So you will skip letters.
Change it with :
for (int counter = 0; counter < line.length(); counter++) {
switch(line.charAt(counter)) {
your problem is in the use the variable i
in your for loop the index counter is i and i is also a variable which you are using to count the occurrences of alphabet 'i'. use this main method, it will work.
public static void main(String[] args) throws FileNotFoundException{
File file = new File("t.txt");
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) {
String line = scan.nextLine();
line = line.toLowerCase();
for (int index = 0; index < line.length(); index++) {
switch(line.charAt(index)) {
case 'a': a++;break;
case 'b': b++;break;
case 'c': c++;break;
case 'd': d++;break;
case 'e': e++;break;
case 'f': f++;break;
case 'g': g++;break;
case 'h': h++;break;
case 'i': i++;break;
case 'j': j++;break;
case 'k': k++;break;
case 'l': l++;break;
case 'm': m++;break;
case 'n': n++;break;
case 'o': o++;break;
case 'p': p++;break;
case 'q': q++;break;
case 'r': r++;break;
case 's': s++;break;
case 't': t++;break;
case 'u': u++;break;
case 'v': v++;break;
case 'w': w++;break;
case 'x': x++;break;
case 'y': y++;break;
case 'z': z++;break;
}
}
}
print();
}
also dont forget to close the scanner after you are done.

Categories