Java Switch Can not find Symbol Error - java

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.

Related

Issues printing a dash on Java

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

Continues to loop even when user enters correct character

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

Convert phonenumber(string) to integer number (Revised) [closed]

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

Syntax error on boolean declaration [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have to keep it in the case 'A' Format i just don't understand what I should have in the (char c) because that seems to be where the error is coming from. If anyone has any suggestions it would be greatly appreciated.
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
areacode = 0;
number = 0;
ext = 0;
}
PhoneNumber(int newnumber) {
areacode = 216;
number = newnumber;
ext = 0;
}
PhoneNumber(int newarea, int newnumber, int newext) {
areacode = newarea;
number = newnumber;
ext = newext;
}
PhoneNumber(String newnumber) {
String areacode = str[0];
String number = str[1];
String[] str = newnumber.split("-");
String[] number = newnumber;
boolean b1, b2;
int i = 0;
int place = 0;
for (int x: newnumber){
newnumber.charAt[i] = place;
b1 = Character.isDigit(place);
if (b1 == true){
number = place;
i++;
} else {
b2 = Character.isLetter(place);
} if (b2 == true) {
number = decode(place);
i++;
} else {
System.out.print("invalid phone number!");
}
}
System.out.print(areacode.concat(number));
return newnumber;
}
private String decode(place) {
switch (c) {
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'z': return "9";
default: return "";
}
}
public boolean equals(PhoneNumber pn) {
//not complete
}
public String toString() {
//not complete
}
}
Here is the error:
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:53: error: <identifier> expected
private String decode(place) {
^
1 error
You have one error that jump out at me. Note that if you have a syntax error, most compilers will freak out and mark everything past the actual error as errors. The best way to debug accidental syntax errors is to look at the first error :)
boolean = b1, b2;
Makes no sense. a variable declaration is as follows
[Type] [Variable Name] | ,[Additional Vars];
so it should be boolean b1, b2;
private String decode(char c) {
switch (c) {
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'z': return "9";
default: return "";
}
}
I suggest you use a lookup table(hashmap) to do this set of code, it looks much less ugly and is easier on sore eyes.
An example of that would be
private static final Map<Char, String> myDecodeLookup = new Hashmap<Char,String>();
static{ //initializer block
myDecodeLookup.put('A', "2");
myDecodeLookup.put('B', "2");
//and so and and so forth
}
private String decode(char c) throws KeyNotFoundException{
return MyClassName.myDecodeLookup.get(c); //you should code this to support the keynotfoundexception that this might throw, or not.
}
If you ever find yourself debugging a syntax error for 5 hours, you should probably take a step back and look up the syntax of the language constructs themselves... that's good advice to take going forward.

Objects - What am I doing wrong?

I am attempting to create an array of objects, Trees (like a forest) 12x12. They are all trees at first. The user inputs the number of fires to start and the probability of the fires spreading to one of the surrounding objects. I have it working when I use an array[][] but when trying to make it work with objects I cannot get the objects to check their surrounding objects then decide to catch fire or not. I am new to the site so I am unsure exactly what you need to see.
public static void fireCheck(Tree[][] trees1){
int i = 0;
int c = 0;
for(i = 0; i <= 11; i ++) {
for(c = 0; c <= 11; c ++) {
switch(trees1[i][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c].burned(); break;
default: break;
}
if(trees1[i][c].getStatus() == '^'){
if(i <= 10){
switch(trees1[i + 1][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c].probability(); break;
default: break;
}
if(c <= 10){
switch(trees1[i][c + 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c+1].probability(); break;
default: break;
}
switch(trees1[i + 1][c + 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c+1].probability(); break;
default: break;
}
}
}
if(i >= 1){
switch(trees1[i - 1][c].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i-1][c].probability(); break;
default: break;
}
if(c >= 1){
switch(trees1[i][c - 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i][c-1].probability(); break;
default: break;
}
switch(trees1[i - 1][c - 1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i-1][c-1].probability(); break;
default: break;
}
}
}
if(i <= 10){
if(c >= 1){
switch(trees1[i+1][c-1].getStatus()){
case '^': break;
case '.': break;
case '*': trees1[i+1][c-1].probability(); break;
default: break;
}
}
}
}
}
}
}
I suppose you want to make this program more object oriented.
Create a class Tree, which has some position and a way of identifying if another tree is neighbor.
interface OurTree{
public int getPosition();
public boolean isNeighbour(OurTree tree);
public boolean isBurning();
}
Implementing above interface will help you.
Create a set of objects of the above implemented interface (assume this as forest). Loop through the set, and identify if any tree is on fire. if yes, burn its neighbours.
Alternatively you can define a method to return neighbours of given tree, and burn them all at once.

Categories

Resources