Java: Cannot find a method's symbol even though that method is declared later in the class. The remaining code is looking for a class.
Posted
by
Midimistro
on Stack Overflow
See other posts from Stack Overflow
or by Midimistro
Published on 2013-11-06T03:50:07Z
Indexed on
2013/11/06
3:53 UTC
Read the original article
Hit count: 173
This is an assignment that we use strings in Java to analyze a phone number. The error I am having is anything below tester=invalidCharacters(c); does not compile because every line past tester=invalidCharacters(c); is looking for a symbol or the class. In get invalidResults, all I am trying to do is evaluate a given string for non-alphabetical characters such as *,(,^,&,%,@,#,), and so on.
What to answer: Why is it producing an error, what will work, and is there an easier method WITHOUT using regex.
Here is the link to the assignment: http://cis.csuohio.edu/~hwang/teaching/cis260/assignments/assignment9.html
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
/////Constructors/////
//Third Constructor (given one string arg) "xxx-xxxxxxx" where first three are numbers and the remaining (7) are numbers or letters
public PhoneNumber(String newNumber){
//Note: Set default ext to 0
ext=0;
////Declare Temporary Storage and other variables////
//for the first three numbers
String areaCodeString;
//for the remaining seven characters
String newNumberString;
//For use in testing the second half of the string
boolean containsLetters;
boolean containsInvalid;
/////Separate the two parts of string/////
//Get the area code part of the string
areaCodeString=newNumber.substring(0,2);
//Convert the string and set it to the area code
areacode=Integer.parseInt(areaCodeString);
//Skip the "-" and Get the remaining part of the string
newNumberString=newNumber.substring(4);
//Create an array of characters from newNumberString to reuse in later methods for
int length=newNumberString.length();
char [] myCharacters= new char [length];
int i;
for (i=0;i<length;i++){
myCharacters [i]=newNumberString.charAt(i);
}
//Test if newNumberString contains letters & converting them into numbers
String reNewNumber="";
//Test for invalid characters
containsInvalid=getInvalidResults(newNumberString,length);
if (containsInvalid==false){
containsLetters=getCharResults(newNumberString,length);
if (containsLetters==true){
for (i=0;i<length;i++){
myCharacters [i]=(char)convertLetNum((myCharacters [i]));
reNewNumber=reNewNumber+myCharacters[i];
}
}
}
if (containsInvalid==false){
number=Integer.parseInt(reNewNumber);
}
else{
System.out.println("Error!"+"\t"+newNumber+" contains illegal characters. This number will be ignored and skipped.");
}
}
//////Primary Methods/Behaviors///////
//Compare this phone number with the one passed by the caller
public boolean equals(PhoneNumber pn){
boolean equal;
String concat=(areacode+"-"+number);
String pN=pn.toString();
if (concat==pN){
equal=true;
}
else{
equal=false;
}
return equal;
}
//Convert the stored number to a certain string depending on extension
public String toString(){
String returned;
if(ext==0){
returned=(areacode+"-"+number);
}
else{
returned=(areacode+"-"+number+" ext "+ext);
}
return returned;
}
//////Secondary Methods///////
//Method for testing if the second part of the string contains any letters
public static boolean getCharResults(String newNumString,int getLength){
//Recreate a character array
int i;
char [] myCharacters= new char [getLength];
for (i=0;i<getLength;i++){
myCharacters [i]=newNumString.charAt(i);
}
boolean doesContainLetter=false;
int j;
for (j=0;j<getLength;j++){
if ((Character.isDigit(myCharacters[j])==true)){
doesContainLetter=false;
}
else{
doesContainLetter=true;
return doesContainLetter;
}
}
return doesContainLetter;
}
//Method for testing if the second part of the string contains any letters
public static boolean getInvalidResults(String newNumString,int getLength){
boolean doesContainInvalid=false;
int i;
char c;
boolean tester;
char [] invalidCharacters= new char [getLength];
for (i=0;i<getLength;i++){
invalidCharacters [i]=newNumString.charAt(i);
c=invalidCharacters [i];
tester=invalidCharacters(c);
if(tester==true)){
doesContainInvalid=false;
}
else{
doesContainInvalid=true;
return doesContainInvalid;
}
}
return doesContainInvalid;
}
//Method for evaluating string for invalid characters
public boolean invalidCharacters(char letter){
boolean returnNum=false;
switch (letter){
case 'A': return returnNum;
case 'B': return returnNum;
case 'C': return returnNum;
case 'D': return returnNum;
case 'E': return returnNum;
case 'F': return returnNum;
case 'G': return returnNum;
case 'H': return returnNum;
case 'I': return returnNum;
case 'J': return returnNum;
case 'K': return returnNum;
case 'L': return returnNum;
case 'M': return returnNum;
case 'N': return returnNum;
case 'O': return returnNum;
case 'P': return returnNum;
case 'Q': return returnNum;
case 'R': return returnNum;
case 'S': return returnNum;
case 'T': return returnNum;
case 'U': return returnNum;
case 'V': return returnNum;
case 'W': return returnNum;
case 'X': return returnNum;
case 'Y': return returnNum;
case 'Z': return returnNum;
default: return true;
}
}
//Method for converting letters to numbers
public int convertLetNum(char letter){
int returnNum;
switch (letter){
case 'A': returnNum=2;return returnNum;
case 'B': returnNum=2;return returnNum;
case 'C': returnNum=2;return returnNum;
case 'D': returnNum=3;return returnNum;
case 'E': returnNum=3;return returnNum;
case 'F': returnNum=3;return returnNum;
case 'G': returnNum=4;return returnNum;
case 'H': returnNum=4;return returnNum;
case 'I': returnNum=4;return returnNum;
case 'J': returnNum=5;return returnNum;
case 'K': returnNum=5;return returnNum;
case 'L': returnNum=5;return returnNum;
case 'M': returnNum=6;return returnNum;
case 'N': returnNum=6;return returnNum;
case 'O': returnNum=6;return returnNum;
case 'P': returnNum=7;return returnNum;
case 'Q': returnNum=7;return returnNum;
case 'R': returnNum=7;return returnNum;
case 'S': returnNum=7;return returnNum;
case 'T': returnNum=8;return returnNum;
case 'U': returnNum=8;return returnNum;
case 'V': returnNum=8;return returnNum;
case 'W': returnNum=9;return returnNum;
case 'X': returnNum=9;return returnNum;
case 'Y': returnNum=9;return returnNum;
case 'Z': returnNum=9;return returnNum;
default: return 0;
}
}
}
Note: Please Do not use this program to cheat in your own class. To ensure of this, I will take this question down if it has not been answered by the end of 2013, if I no longer need an explanation for it, or if the term for the class has ended.
© Stack Overflow or respective owner