I have been stuck at this for sometime and was wondering if anyone would be able to guide me.
I am trying to let the user enter in a character and if it does not match one of the switch cases, then it should continue to ask the user. My code does that part fine. The issue comes up when the user tries to enter a correct character after and my code continues to output the default instead of the specific case.
Any help would be appreciated!
Thank you
while(!flag){
switch (endChar) {
case 'T':
case 't':
modFlags[0] = true;
flag = true;
break;
case 'P':
case 'p':
modFlags[1] = true;
flag = true;
break;
case 'W':
case 'w':
modFlags[2] = true;
flag = true;
break;
case 'L':
case 'l':
modFlags[3] = true;
flag = true;
break;
case 'I':
System.out.print("sd");
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'i':
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'O':
case 'o':
break;
case 'D':
case 'd':
break;
case 'M':
case 'm':
modeBoth = true;
flag = true;
break;
case 'H':
case 'h':
showMenu = false;
flag = true;
break;
default:
System.out.println("Unknown Option.");
System.out.print("Enter action: ");
sc.next();
break;
// flag = false;
}
}
Change
sc.next();
near the bottom of the switch to
endChar = sc.next().charAt(0);
because all you're currently doing is retrieving a String from the Scanner and not doing anything with it. You need to assign a character of that String to endChar if you want the value of endChar to be different for the next iteration of the loop.
Based on what you provided, I suggest moving the scanning a value portion to the very beginning of your while loop and in the default case of your switch statement using just a continue; to bring you back to the beginning of the while loop. That way it will read in another value from input without having to code it again.
while(!flag){
System.out.print("Enter action: ");
endChar = sc.next().charAt(0);
switch (endChar) {
case 'T':
case 't':
modFlags[0] = true;
flag = true;
break;
case 'P':
case 'p':
modFlags[1] = true;
flag = true;
break;
case 'W':
case 'w':
modFlags[2] = true;
flag = true;
break;
case 'L':
case 'l':
modFlags[3] = true;
flag = true;
break;
case 'I':
System.out.print("sd");
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'i':
fileName = updateFileName(sc, fileName);
flag = true;
break;
case 'O':
case 'o':
break;
case 'D':
case 'd':
break;
case 'M':
case 'm':
modeBoth = true;
flag = true;
break;
case 'H':
case 'h':
showMenu = false;
flag = true;
break;
default:
continue;
}
}
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
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]);
}
}
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 :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
Question
Write a method that returns a number, given an uppercase letter, as follows.
int getNumber (char uppercaseLetter)
Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (uppercase or lowercase) to a digit and leaves all other characters intact.
Sample run from textbook
Enter a string: 1-800-Flowers
1-800-3569377
Enter a string: 1800flowers
18003569377
Here is what I have so far
import java.util.Scanner;
public class Assignment {
public static int correspondingNumber(char uppercaseLetter){
int correspondingNumber=0;
switch (uppercaseLetter)
{
case 'A':
case 'B':
case 'C': correspondingNumber=2; break;
case 'D':
case 'E':
case 'F': correspondingNumber=3; break;
case 'G':
case 'H':
case 'I': correspondingNumber=4; break;
case 'J':
case 'K':
case 'L': correspondingNumber=5; break;
case 'M':
case 'N':
case 'O': correspondingNumber=6; break;
case 'P':
case 'Q':
case 'R':
case 'S': correspondingNumber=7; break;
case 'T':
case 'U':
case 'V': correspondingNumber=8; break;
case 'W':
case 'X':
case 'Y':
case 'Z': correspondingNumber=9; break;
}
return correspondingNumber;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num;
char num1 = 0;
System.out.print("Enter a string: ");
num = input.next();
num.toUpperCase();
int i=0;
while(i!=num.length()){
num1=num.charAt(i);
}
System.out.print(correspondingNumber(num1));
}
}
steps need tobe done
scan input let's say as String
Convert string to character array (srcArray)
change method return of correspondingNumber to Char
default return to input and apply switch case
call method correspondingNumber, store return char in stringbuilder or Array of Char array
repeat step-5 until character array(srcArray) is completely processed
print the output
import java.util.Scanner;
public class Assignment {
// changed return type
public static char correspondingNumber(char uppercaseLetter) {
char correspondingNumber = uppercaseLetter;// default the return value
// to input
switch (uppercaseLetter) {
case 'A':
case 'B':
case 'C':
correspondingNumber = '2';
break;
case 'D':
case 'E':
case 'F':
correspondingNumber = '3';
break;
case 'G':
case 'H':
case 'I':
correspondingNumber = '4';
break;
case 'J':
case 'K':
case 'L':
correspondingNumber = '5';
break;
case 'M':
case 'N':
case 'O':
correspondingNumber = '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
correspondingNumber = '7';
break;
case 'T':
case 'U':
case 'V':
correspondingNumber = '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
correspondingNumber = '9';
break;
}
return correspondingNumber;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String num;
char num1 = 0;
System.out.print("Enter a string: ");
num = input.next();
num.toUpperCase();
int i = 0;
while (i != num.length()) {
num1 = num.charAt(i);
System.out.print(correspondingNumber(num1)); // moved print
// statement to
// appropriate place
i++; // iterate loop
}
input.close();
}
}
I have a problem in java. It can not find the symbol in my switch function.
I think there is something wrong with char inputChar(i) but I can not figure out the problem.
Here is the code:
import eip.*;
public class IntDigitAutomat
{
public static boolean digitCheck(char inputChar)
{
boolean isdigit = true;
int q = 1;
for (int i=0; i<input.length(); i++)
{
if(isdigit == false)
break;
switch(q)
{
case 1:
switch(inputChar(i))
{
case '+':
case '-':
isdigit = true;
q=2;
break;
case ' ':
isdigit = true;
q=1;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
q=3;
break;
default:
isdigit = false;
}
case 2:
switch(inputChar(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
q=3;
break;
default:
isdigit = false;
}
case 3:
switch(inputChar(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
isdigit = true;
break;
default:
isdigit = false;
}
}
return isdigit;
}
}
}
I needed more text. I needed more text. I needed more text. I needed more text.
you have to pass single character to your switch statement, change switch(inputChar(i)) to switch(inputChar)
for (int i=0; i<input.length(); i++)
This is where you are getting a problem where is the string or literal that you are supposed to be searching? where is the String input?
If you are trying to create some kind of math expression parser you are going about it the wrong way.
You should look on google or even other questions here about math parsing.
Also look at the other answer about your inputChar(i) which is also going to solve another error you are getting.
your code is not proper if you are using inputchar array then in method attribute take it as array if its not array then loop condition is wrong as well as switch condition is wrong.