Why is wrong with my code???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krabina1988
    New Member
    • Oct 2011
    • 9

    Why is wrong with my code???

    why is it that when I tried char as input its not printing WORD FOUND.

    Code:
    import java.io.*;
    
    public class fs
    {
        public static void main(String args[]) throws IOException
        {
            String [] datatype = new String[4];//{"boolean","byte","char","double","float","int","long","short"};
            String input;
            //int i=0;
            BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter String: ");
            input = buff.readLine();
            
            datatype[0]="boolean";
            datatype[1]="byte";
            datatype[2]="char";
            datatype[3]="double";
            
            
            for(int i=0;i<datatype.length;i++)
            {
                if(input==datatype[i])
                {
                    System.out.println("WORD FOUND");
                    i=datatype.length;
                }
                else
                {
                    System.out.println("WORD NOT FOUND");
                }
            }
        }
    }
  • veera123
    New Member
    • Dec 2011
    • 2

    #2
    Hi,
    use equals method to compare objects.Don't use == to compare the string objects

    Comment

    Working...