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 07-09-2008, 06:02 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
Entering a while loop with a not equal to string
I am trying to enter a while loop when a string is not equal to "stop". However, I cannot seem to get it work correctly. I have tried using:

while (!variable.equalsIgnoreCase ("stop") )

and it does not work. I have also tried:

while variable != "stop" // even though != is not a string operator.

I either end up entering the loop when I enter "stop" or I end up with an infinite loop - which really stinks. I have Googled my brains out and can't find anything on entering a while loop if a string is not equal to another string. I have found information for if...else statements, but I am not allowed to use if...else statements. I have to use a while statement.

If anyone could help I would very, very appreciative!

Brian
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-09-2008, 06:08 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
Eku is on a distinguished road
Can you post your code please ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-09-2008, 06:11 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Did you try that is equal. If so check the value of variable
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-09-2008, 06:22 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
Sure. Here is the entire code. It is a simple payroll program that uses Scanner to input employee name (the variable in question), hourly rate and hours worked and displays the employee's name and gross pay. No overtime calculations are required. I want to exit the program when stop is entered and display an exit message. Here is the code:

// Payroll Program by Brian M. King
// This program calculates an employee's weekly pay
// Week 3 Assignment IT 215

import java.util.Scanner;

public class Payroll2
{
public static void main( String args [] )
{
String employeeName; // Employee's name
double hours; // number of hours worked
double hourlyRate; // employee's hourly rate of pay
double weeklyPay; //calculation of employee's weekly pay

//create scanner to obtain input from command window
Scanner input = new Scanner( System.in );

System.out.print( "Enter the employee's name. Enter 'stop' to quit:" ); //prompt
employeeName = input.next (); // read employee name

while ( !employeeName.equalsIgnoreCase ("stop") );
{
System.out.print( "Enter the number of hours the employee worked: " ); // prompt
hours = input.nextDouble (); // read number of hours

while ( hours < 0 ) // verify a positive value for hours worked
{
System.out.print ( "Please enter a positive value for hours worked:" );// error message
hours = input.nextDouble (); // new value for hours
}

System.out.print( "Enter the employee's hourly rate of pay: $" ); // prompt
hourlyRate = input.nextDouble (); // read employee rate of pay

while (hourlyRate < 0 )
{
System.out.print( "Please enter a positive value for hourly rate of pay:" );// error message
hourlyRate = input.nextDouble ();// new value for hourly rate
}

weeklyPay = hours * hourlyRate; // calculate weekly pay

System.out.printf( "Employee %s", employeeName ); // display employee name
System.out.printf( " earned $%.2f\n", weeklyPay ); // display weekly pay
}

System.out.println( "Thank you for using the Payroll Program!" ); // exit message

} // end method main

} // end class Payroll2
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 06:27 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
Eku is on a distinguished road
You have a very simple error ^_^

please change this:
Code:
while ( !employeeName.equalsIgnoreCase ("stop") );
to This:
Code:
while ( !employeeName.equalsIgnoreCase ("stop") )
You put a ";". ^_^ Try removing it ^_^

I hope that helps
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-09-2008, 06:48 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
Eku is on a distinguished road
Just found a bug on your code.

If i entered a name, it will loop infinitely asking for rate nad number of hours for that same employee.

Try fixing that one ^_^ That is another logic error in your code.

Clue: It has something to do with the line the inputs the employee name and ofcourse the while loop ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-09-2008, 06:50 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Additional information. Such an error should pointed in a Java IDE. Not exactly you get the error explanation there. But just looking at the error message you should have an idea what happened. Isn't it Eku?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-09-2008, 06:58 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
Eku is on a distinguished road
yup the error gives a lot of Clue, but in this case there is no error messages. it is purely Logical error in his loop. ^_^ He can figure that one. =P His Loops going infinitely if you entered a name. ^^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-09-2008, 07:15 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
Thanks so much for your help - with the semi-colon and not prompting for a name again! I really thought I was going to pull my hair out!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-09-2008, 08:10 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
Eku is on a distinguished road
No Problem ^_^ just try fixing that bug and ask again if your stuck at something ^^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
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
How to stop entering Data adeeb AWT / Swing 1 06-07-2008 03:58 AM
checking if there are equal numbers nalinda New To Java 1 11-18-2007 07:21 AM
checking if there are equal numbers nalinda New To Java 0 11-18-2007 03:13 AM
i need assistance with a string triggered loop please! Phobos0001 New To Java 9 11-14-2007 03:44 PM
terminating a while loop with a string tkdvipers New To Java 3 07-10-2007 12:23 AM


All times are GMT +3. The time now is 01:48 AM.


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