codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  While Loops - Help please!  wat_unavailable at 14:33 on Tuesday, March 27, 2007
 

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

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");
}
}
}



  Re: While Loops - Help please!  crwood at 07:44 on Wednesday, March 28, 2007
 

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

// change this
name==""
// to this
name.equals("")









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Hi need help in time()Fn
•  Help me out in wirting a perl script
•  how to get the ip address & country of the webpage visitor
•  Firefox
•  Re: Perl - Read and Write output file
•  Re: Print and print preview file on the website without using the File - Print on the IE
•  Re: While Loops - Help please!
•  trying to write dates from perl to Excel
•  5x5 Tic Tac Toe


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007