Write a function that accepts one parameter and test to see

Write a function that accepts one parameter and test to see if the input parameter to the function is a legitimate java identifier.

Write a class which has two methods:

legitimateJavaIdentifiter (String parameter) {//statement to check parameter}

main function to test the legitimateJavaIdentifiter (String parameter) function.

•Output the results

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HINT:

•Main (){

String parameter; // ask user to type the parameter from keyboard or initialize the parameter by assignment.

Function legitimateJavaIdentifiter (Parameter);

// output the results

}

boolean LegitimateJavaIdentifiter (String parameter){

// Returns TRUE if parameter is a legal Java identifier,

// Otherwise returns FALSE

if (parameter is identifier) // The elements in the identifier follow the rules

//hints: check the Unicode of characters

       return true;

else

    return false;

}

Java Identifiers All Java components require names. Names used for classes, variables, and methods are called identifiers. In Java, there are several points to remember about identifiers. They are as follows All identifiers should begin with a letter (A to Z or a to z), currency character or an underscore After the first character, identifiers can have any combination of characters. A key word cannot be used as an identifier. Most importantly, identifiers are case sensitive. o Examples of legal identifiers: age, $salary, Value, 1 value Examples of illegal identifiers 123abc, salary

Solution

Before going to read about these inbuild methods of java

1)public static boolean isJavaIdentifierPart(String s)

This method returns true if the character may be part of a Java identifier, false otherwise.

2)public static boolean isJavaIdentifierStart(char ch)

This method returns true if the character may start a Java identifier, false otherwise.

Note:

Syntax of java keyword and java identifier is same.

So, in order to differentiate between keyword and identifier ,we have to compare it with list of possible keywords of java.

Code:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {    
    Scanner sc=new Scanner(System.in);
    String s=sc.next();
    boolean f=isValidJavaIdentifier(s);
    System.out.println(f);
    sc.close():
  
    }
  
     public final static boolean isValidJavaIdentifier(String s)
{
       
         String[] keywords = {
        \"assert\",
        \"abstract\", \"boolean\", \"break\", \"byte\",
        \"case\", \"catch\", \"char\", \"class\",
        \"const\", \"continue\", \"default\", \"do\",
        \"double\", \"else\", \"extends\", \"final\",
        \"finally\", \"float\", \"for\", \"goto\",
        \"if\", \"implements\", \"import\",
        \"instanceof\", \"int\", \"interface\",
        \"long\", \"native\", \"new\", \"package\",
        \"private\", \"protected\", \"public\",
        \"return\", \"short\", \"static\", \"super\",
        \"switch\", \"synchronized\", \"this\",
        \"throw\", \"throws\", \"transient\",
        \"try\", \"void\", \"volatile\", \"while\"
        };
       
   
         
     // an empty or null string cannot be a valid identifier
     if (s == null || s.length() == 0)
     {
        return false;
     }

     char[] c = s.toCharArray();
      
         // java in buit method to check First character in valid java identifier or not
     if (!Character.isJavaIdentifierStart(c[0]))
     {
        return false;
     }

     for (int i = 1; i < c.length; i++)
     {
       
        if (!Character.isJavaIdentifierPart(c[i]))
        {
           return false;
        }
     }

       //comparing it with all keywords
       
         for (int i = 0; i < keywords.length; ++i) {
    if (s.equals(keywords[i])) {
     
     return false;
    }
}
       
       
     return true;
}

  
}

Write a function that accepts one parameter and test to see if the input parameter to the function is a legitimate java identifier. Write a class which has two
Write a function that accepts one parameter and test to see if the input parameter to the function is a legitimate java identifier. Write a class which has two
Write a function that accepts one parameter and test to see if the input parameter to the function is a legitimate java identifier. Write a class which has two

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site