Question

I am having a problem with my String toString() method. Here is the code for the whole program (Keep in mind please I am new to the java world, so please explain any answers, thanks)

import static java.lang.System.*;
import java.util.Scanner;
public class CharacterAnalyzer
{
private char theChar;
private boolean isUpper, isLower, isNumber;
public CharacterAnalyzer()
{
    char theChar = ' ';
}
public CharacterAnalyzer(char c)
{
    theChar = c;
}
public void setChar(char c)
{
    theChar = c;
}
public char getChar()
{
    char getChar= theChar;
    return getChar;
}
public boolean isUpper( )
{
    if (((int)theChar)>64&&((int)theChar)<91){
        boolean isUpper = true;
        return isUpper;
    }   
    else{
        boolean isUpper = false;
        return isUpper;
    }
}
public boolean isLower( )
{

    if (((int)theChar)>97&&((int)theChar)<123){
        boolean isLower = true;
        return isLower;
    }   
    else{
        boolean isLower = false;
        return isLower;
    }
}
public boolean isNumber( )
{
    if (((int)theChar+48)>47&&((int)theChar+48)<58){
        boolean isNumber = true;
        return isNumber;
    }   
    else{
        boolean isNumber = false;
        return isNumber;
    }

    }
public int getASCII( )
{
        return ((int)theChar);
}
public String toString()
{

    if(isNumber == true && isUpper == false && isLower == false){
        String chartype = "a number";
        return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
    }
    else if(isNumber == false && isUpper == true && isLower == false){
        String chartype = "an uppercase character";
        return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
    }
    else if(isNumber == false && isUpper == false && isLower == true){
        String chartype = "a lowercase character";
        return ""+getChar() + " is "+chartype+". ASCII == " + getASCII() + "\n";
    }

}
}

Here is the runner also:

import static java.lang.System.*;
import java.util.Scanner;

public class Lab05d
{
    public static void main ( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        out.print("Enter a letter :: ");
        char letter = keyboard.next().charAt(0);
        CharacterAnalyzer test = new CharacterAnalyzer(letter);
        out.println(test);   //A

        //add more test cases

        out.print("Enter a letter :: ");
        char letter2 = keyboard.next().charAt(0);
        CharacterAnalyzer test2 = new CharacterAnalyzer(letter2);
        out.println(test2);   //l

        out.print("Enter a letter :: ");
        char letter3 = keyboard.next().charAt(0);
        CharacterAnalyzer test3 = new CharacterAnalyzer(letter3);
        out.println(test3);   //a

        out.print("Enter a letter :: ");
        char letter4 = keyboard.next().charAt(0);
        CharacterAnalyzer test4 = new CharacterAnalyzer(letter4);
        out.println(test4);   //7

        out.print("Enter a letter :: ");
        char letter5 = keyboard.next().charAt(0);
        CharacterAnalyzer test5 = new CharacterAnalyzer(letter5);
        out.println(test5);   //D

        out.print("Enter a letter :: ");
        char letter6 = keyboard.next().charAt(0);
        CharacterAnalyzer test6 = new CharacterAnalyzer(letter6);
        out.println(test6);   //2

        out.print("Enter a letter :: ");
        char letter7 = keyboard.next().charAt(0);
        CharacterAnalyzer test7 = new CharacterAnalyzer(letter7);
        out.println(test7);   //X

        out.print("Enter a letter :: ");
        char letter8 = keyboard.next().charAt(0);
        CharacterAnalyzer test8 = new CharacterAnalyzer(letter8);
        out.println(test8);   //Z

        out.print("Enter a letter :: ");
        char letter9 = keyboard.next().charAt(0);
        CharacterAnalyzer test9 = new CharacterAnalyzer(letter9);
        out.println(test9);   //9


    }
}

I am pretty sure the error is just in the String toString method, at least thats what bluej is telling me. I've looked around, and other people just put an else statement on their code, but when I do, it always executes the else statement, and bypasses my ifs (I dont know why, I'm putting them on the same indent...). I also tried putting a return outside the ifs/elses entirely, and when I compiled it, it didnt find the variables I used in my toString method, the isUpper isLower and isNumber values. When my teacher assigned this, he gave us the input we should use to test it, A, 1, a, 7, D, 2, X, z, 9 So, how do I get it to run and output their ASCII values, 65,49,97,55,68,50,88,122,57 (I've gone over this stupid problem so many times their values are imprinted in my mind...). I'm getting really frustrated, please help me out!

Actually I just thought of something. I think it may be not finding my previous methods (isUpper, isLower, and isNumber), which would just execute their else statements, making them all false, thereby making all my ifs invalid... Anyone know how to fix this too?

Was it helpful?

Solution 2

You've got the following:

if (...)
    return ...
else if (...)
    return ...
else if (...)
    return ...

Question: What happens if none of the if conditions match?

Answer: There'd be a horrible error, since there's no return statement after the final else if clause. As the above answer says, it doesn't matter that you know there is no way it can fall off the end - the compiler does not know that. To fix the error, you need to do one of the following things:

if (...)
    return ...
else if (...)
    return ...
else if (...)
    return ...
else
    return ...

or

if (...)
    return ...
else if (...)
    return ...
else if (...)
    return ...
return ...

Note that the second answer has a return statement below all the clauses. This is a minor difference, and it doesn't matter as far as your question goes, but if, for example, one of the if clauses had another conditional inside it, that could the fail to return something. For example:

if (...)
    if (...)
        return ...
    else
        ...
else if (...)
    return ...
else if (...)
    return ...
else
    return ...

The above would also fail to compile, since there is no return statement in the nested else clause.

OTHER TIPS

Your toString method takes the form of

if (a) {
    // return stuff
} else if (b) {
    // return stuff
} else if (c) {
    // return stuff
}

There's no guaranteed path of execution with a return statement in it. Even if logic dictates that c will always be true when a and b are both false, this isn't good enough for the compiler.

If it is the case that c will always be true when a and b are both false, then you should change else if (c) to simply else.

Or, you could throw this at the end of the method:

return "";

Or you can add to the current method:

} else {
    // throw an exception
}

Basically, there's no garantee that a return statement will be reached in your code.

You coud refactor it:

public String toString()
{
    // declare variable to store return value, initialize it to ¿empty string?
    // if - elseif logic to assign proper value to the variable
    // return the string variable 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top