0
public boolean isJavaFile(String str1){

        String str="";
        for(int i=0;i<str1.length();i++){
            char ch=str1.charAt(i);
            if(ch!='.')
            return false;
            else
            str=str+ch;


        }
        if(str == .java)
        return true;
        return false;


    }   

I want to do logic, without using other string functions like if i want to solve it i can solve it by using endsWith().

markspace
  • 10,621
  • 3
  • 25
  • 39
Pallavi Singh
  • 133
  • 10
  • 4
    String comparison is done using `equals()`. [Read about it](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) before you keep on trying! – Nir Alfasi Aug 09 '15 at 06:08
  • Why do you not want to use `endsWith`? – slartidan Aug 09 '15 at 06:08
  • Suppose the String has 5000 characters, and you want to know if it ends with ".java". Is it really necessary to iterate though the first 4995 characters? Wouldn't that be simpler to just check if the 4996th char is ., the 4997th is j, etc.? – JB Nizet Aug 09 '15 at 06:12
  • Also, suppose a string has 3 characters. Is it possible for that string to end with ".java"? No, since ".java" has 5 characters. Don't forget to check for boundary conditions like this. – markspace Aug 09 '15 at 06:14
  • Given the way you've worded the title, all files must be Java files. Do you mean 'the name of a Java file ends with .java'? – user207421 Aug 09 '15 at 06:42

3 Answers3

1

There is a error in your if statement. Since .java is a string,it should be enclosed in double quotes. It should be ".java" and your code should be as follows

public boolean isJavaFile(String str1){

    String str="";
    for(int i=0;i<str1.length();i++){
        char ch=str1.charAt(i);
        if(ch!='.')
            continue;
        else 
            while(i!=str1.length()){
                str=str+str1.charAt(i);
        i++;
            }
    }

    if(str.equals(".java"))
    return true;
    return false;


}   
pramod kumar
  • 275
  • 2
  • 12
0

used this code :-

public boolean isJavaFile(String filename)
{
    int index = filename.lastIndexOf(".");
    String str = "";
    for(int i=index;i<=filename.length() -1;i++)
    {
        str += filename.charAt(i);
    }

    return str.equals(".java");
}
HARDIK SHAH
  • 163
  • 1
  • 14
  • 2
    You're lucky that "real" .java files can't be, for example Foo.Bar.java. Why use equalsIgnoreCase()? .JAVA is not a valid Java file extension. And the last 8 lines should be reduced to `return str.equals(".java");` – JB Nizet Aug 09 '15 at 06:24
0

You can do it in reverse order.

public boolean isJavaFile(String str1) {
    String str="";
    for(int i=str1.length()-1; i >=0; i--){
        char ch = str1.charAt(i);
        if(ch=='.') {
            str=str+ch;            
            break;
        }
        else
            str=str+ch;            
    }

    if(str.equals("avaj."))
        return true;
    return false;        
}
d_air
  • 631
  • 1
  • 4
  • 12
  • You can still add more optimization by changing the last 3 codes to just `return str.equals("avaj.");` as pointed out by @JB Nizet in the comment of the other answer. – d_air Aug 09 '15 at 06:46