Results 1 to 3 of 3
  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Thumbs down problem with using string in if condition

    Hi,i wrote code for stack operations.
    I pasted that code below,
    Java Code:
    import java.io.*;
    class Stack
    {
     int i;
     int[] st;
     
     public Stack(int size)
     {
       st=new int[size];
       i=-1;
     }
     public void push()throws IOException
     {
      i++;
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter a element:");
      st[i]=Integer.parseInt(br.readLine());
      
     } 
     public void pop()
     {
       if(i<0)
        System.out.println("Stack is empty.");
       else
      {
        i--;
       System.out.print("Last entered number is deleted.");
      }
     }
     public void checkempty()
     {
      if(i<0)
       System.out.println("Stack is empty.");
      else
       System.out.println("Stack is not empty.");
      }
      public void noofelements()
      {
       System.out.println("Number of elements in the Stack :"+(i+1));
      }
      public void printelements()
      {
       
       if(i<0)
        System.out.println("Stack is empty.");
       else
        {
          System.out.println("Stack elements are :");  
          for(int j=0;j<=i;j++)
            System.out.println(st[i]+" ");
         }
      }
    }
    
    
    class StackOperations
    {
     public static void main(String[] args)throws IOException
     {
      int a,size;
      String check;
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
      System.out.print("Enter the length of the stack:");
      size=Integer.parseInt(br.readLine());
    
      Stack stobject=new Stack(size);
    
      System.out.println("This program is to perform stack operations.\n");
      System.out.println("Options for each operation;");
      System.out.println("1.insertion.");
      System.out.println("2.deletion.");
      System.out.println("3.to check stack is empty or not.");
      System.out.println("4.to print number of elements in stack.");
      System.out.println("5.to print stack elements.");
      do
      {
       System.out.print("\nEnter Your option:");
       a=Integer.parseInt(br.readLine());
       System.out.print("\n");
       switch(a)
       {
        case 1:  //insertion.
        {
          stobject.push();break;
        }
        case 2:  //deletion.
        {
          stobject.pop();break;
        }
        case 3:  //check empty.
        {
          stobject.checkempty();break;   
        }
        case 4:  //stack elements number.
        {
         stobject.noofelements();break;
        }
        case 5:  //to print stack elements. 
        {
         stobject.printelements();break;
        }
       }
       System.out.print("\nDo you want to continue(Enter yes/no) :");
       check=br.readLine();
      }while(check=="yes");
     }
    }
    With the above program i didn't get any errors.
    But even though i entered yes for checking do-while loop,it comes out of the loop.
    Is there any mistake in my code ?
    And was i followed correct way to insertion, deletion and also for all other methods ?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Is there any mistake in my code
    Java Code:
    while(check=="yes");
    // Better to test for String equality with [i]equals[/i]
    while(check.equals("yes"));

  3. #3
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

Similar Threads

  1. Problem with String Building
    By Albert in forum New To Java
    Replies: 2
    Last Post: 04-30-2012, 01:49 AM
  2. String and Arry Problem
    By farakhkhan@yahoo.com in forum New To Java
    Replies: 3
    Last Post: 03-31-2008, 11:17 AM
  3. JFrame (closing under a condition)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:17 PM
  4. Replies: 0
    Last Post: 11-20-2007, 05:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •