Results 1 to 2 of 2
  1. #1
    trill is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with loop in java

    Hi, Can anyone help me. I cant get either my resident (y/n) or name (error message to work.

    Java 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.

  2. #2
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Use the equals method for testing String equality. The == operator won't work.

    Java Code:
    // change this
    name==""
    // to this
    name.equals("")

Similar Threads

  1. Loop Help
    By HeavyD in forum New To Java
    Replies: 7
    Last Post: 09-22-2010, 10:55 PM
  2. Replies: 0
    Last Post: 11-14-2007, 04:22 PM
  3. Help, loop with java
    By cachi in forum New To Java
    Replies: 5
    Last Post: 08-01-2007, 07:03 AM
  4. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 05:26 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
  •