In: Computer Science
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and 3 could represent char). The method returns true if all the tokens in the String are of the specified data type; otherwise, it returns false.
Provide a sample output.
here is your class : ----------->>>>>>>>>
class CheckString{
private String data;
private String delimeter;
public CheckString(){
data = "";
delimeter = "";
}
public CheckString(String data,String delimeter){
this.data = data;
this.delimeter = delimeter;
}
public boolean checkTokens(int type){
if(type == 0){
String[] check = data.split(delimeter);
for(String s : check){
if(s.equals("true") || s.equals("false")){
}else{
return false;
}
}
return true;
}else if(type == 1){
String[] check = data.split(delimeter);
for(String s : check){
try{
Integer.parseInt(s);
}catch(Exception e){
return false;
}
}
return true;
}else if(type == 2){
String[] check = data.split(delimeter);
for(String s : check){
try{
Double.parseDouble(s);
}catch(Exception e){
return false;
}
}
return true;
}else if(type == 3){
String[] check = data.split(delimeter);
for(String s : check){
if(s.length() > 1){
return false;
}
}
return true;
}
return false;
}
}