mardi 20 octobre 2020

Simple Program to check whether input String is a keyword or not (keywords are specified)

This is the code I'm trying to execute but I get the same result no matter what the input is, i.e. "<input string> is not a keyword".

Here is the list of keywords: {break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var}

I want to print "<input string> is a keyword" if my input string is present in the list of keywords; else print "<input string> is not a keyword".

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package word.is.key;

/**
 *
 * @author JagritSharma
 */
import java.util.*;
public class WordIsKey {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input= new Scanner(System.in);
        String s= "break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var";
        String[] keywords= s.split(",", 16);
        String n= input.nextLine();
        String res= n+" is not a keyword";
        for(int i=0;i<keywords.length;i++){
            if(keywords[i].equals(n)){
                res= n+" is a keyword";
                break;
            }
        }
        System.out.println(res);
    }
    
}

Aucun commentaire:

Enregistrer un commentaire