Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007, 10:21 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
problem with using string in if condition
Hi,i wrote code for stack operations.
I pasted that code below,
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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 10:54 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Is there any mistake in my code
Code:
while(check=="yes"); // Better to test for String equality with equals while(check.equals("yes"));
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 11:40 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
thank q,it works well
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String and Arry Problem farakhkhan@yahoo.com New To Java 3 03-31-2008 11:17 AM
JFrame (closing under a condition) Java Tip Java Tips 0 03-12-2008 12:17 PM
Using java.util.Scanner to search for a String in a String Java Tip Java Tips 0 11-20-2007 05:59 PM
Problem with String Building Albert New To Java 0 07-09-2007 08:20 PM


All times are GMT +3. The time now is 11:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org