Answer to Question #268016 in Java | JSP | JSF for Candy

Question #268016

package studentBase;

class Test

{

String greet = “Hi”;

String name = “Smedley”;

String nickName = name.substring (0,2); If (nickName == name.substring(0,2))

System.out.println(“has real nickname”);

else if (greet + name == greet + nickName);

System.out.println(“no real nickname”);

Else

System.out.println(“hmmm ... changed names?”); }

Will it compile? YES or NO. Give reason, if No.


1
Expert's answer
2021-11-21T17:39:27-0500


NO, the program will not compile.

This is because, first there is no main method which is defined and in java the execution of any program starts in the main method.


There are still syntax errors, for example the if key word starts with a capital letter and it should be a small letter i i.e. if instead of If. Also the else keyword starts with a capital letter E instead of e i.e. Else instead of else.


THE FOLLOWING I THE CODE THAT WILL COMPILE


package studentBase;

public class Test {

    public static void main(String[] args) {

        String greet = "Hi";

        String name = "Smedley";

        String nickName = name.substring(0, 2);

        if (nickName == name.substring(0, 2))
        {
            System.out.println("has real nickname");
        }
        else if (greet + name == greet + nickName)
        {
            System.out.println("no real nickname");
        }
        else {
            System.out.println("hmmm ... changed names?");
        }

    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS