Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 08-07-2007, 05:18 AM
Member
 
Join Date: Jul 2007
Posts: 40
trill is on a distinguished road
Help with loop in java
Hi, Can anyone help me. I cant get either my resident (y/n) or name (error message to work.

Code:
import java.util.*; public class resident { public static void main (String[] args) { Scanner console = new Scanner(System.in); // Variables String y="yes"; String n="no"; String resident=""; String name=""; double tax=1; double salary; // Name input System.out.print("Please enter name: "); name = console.nextLine(); // Name. Error message if name blank. while(name=="") { if (name=="") { System.out.print("Error: Firstname cannot be blank. Please enter first name again: "); name = console.nextLine(); } else { break; } } // Salary Input System.out.print("Please enter Salary: "); salary = console.nextDouble(); // Resident Status Question System.out.print("Resident status: Please enter 'y' for yes or 'n' for no."); resident = console.nextLine(); // While statement. If resident is yes continue. If resident is no continue. If neither, error message. while((resident==y)||(resident==n)) { if(resident==n) { break; } else if(resident==y) { break; } else { System.out.print("Error: Please enter resident status again."); System.out.print("Please enter 'y' for yes or 'n' for no."); resident = console.nextLine(); } } // Output if(resident==n) { System.out.print("\n"); System.out.print("Name: " +name + "\n"); System.out.print("Resident: No"+ "\n"); tax = 0.1*salary; System.out.print("Tax rate 10% = "+tax + "\n"); System.out.print("Salary: " +salary + "\n"); } else { System.out.print("\n"); System.out.print("Name: " +name + "\n"); System.out.print("Resident: Yes" + "\n"); tax = 0.09*salary; System.out.print("Tax rate 9% = "+tax + "\n"); System.out.print("Salary: " +salary + "\n"); } } }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 09:36 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
Use the equals method for testing String equality. The == operator won't work.

Code:
// change this name=="" // to this name.equals("")
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
Enhanced for-loop (Java 5.0) JavaForums Java Blogs 2 03-31-2008 03:53 PM
Iterating through ArrayList - using newly introduce for loop in Java 5.0 Java Tip Java Tips 0 11-14-2007 05:22 PM
Help, loop with java cachi New To Java 5 08-01-2007 08:03 AM
Loop Help HeavyD New To Java 5 07-10-2007 03:26 PM
Enhanced For loop In Java goldhouse Advanced Java 1 05-06-2007 06:26 PM


All times are GMT +3. The time now is 10:24 PM.


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