How Do I Cotrol the Flow of Execution of this Code ?
HI,
I am trying to write a code that can take all the user information at the command prompt and displays the same entered information. My code works well upto taking theh data and displaying..
But, now i want to put some restrictions like entering valid acceptable data for ex.: for gender only male or female and not anything else.
So I implemented this using the string comparison ignoring the cases just for the gender, using the "==" operator and the if statement.
Now my desire was to check the input data at gender and see if it valid like male/female or not irrespective of cases, and then asking the user to renter that right value for gender. and should not proceed until entered properly. But my code checks it and skips to the next data to be fed !
How do I control that flow of my code ie., it should not go ahead after checking the validity of data entered until the right data is fed ?
MY CODE IS:
import java.io.*;
import java.lang.*;
class Profilev1{
public static void main(String args[]) throws IOException{
String gend=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter your name:\r");
String name=br.readLine();
b1:{System.out.println("Please enter your gender Male/Female :\r");
gend=br.readLine();
if(gend!="male" && gend!="female")
{System.out.println("Please enter either Male or Female\n");
}
break b1;
}
System.out.println("Please enter your age:\r");
int age= Integer.parseInt(br.readLine());
System.out.println("Please enter your salary $Dollars.cents:\r");
float sal=Float.parseFloat(br.readLine());
System.out.println("\n\nThank You "+name+".\n\n\nHere's your profile with us on file :\r");
System.out.println("Name:\t"+name+"\nGender:\t"+gend+"\nAge:\t"+age+"\nSalary:\t"+sal+"\r");
}
}
OUTPUT IS:
C:\Program Files\Java\jdk1.6.0_02\bin>javac Profilev1.java
C:\Program Files\Java\jdk1.6.0_02\bin>java Profilev1
Please enter your name:
Aron Philip
Please enter your gender Male/Female :
male
Please enter either Male or Female
Please enter your age:
100
Please enter your salary $Dollars.cents:
75600.23
Thank You Aron Philip.
Here's your profile with us on file :
Name:Aron Philip
Gender: male
Age:100
Salary: 75600.23
C:\Program Files\Java\jdk1.6.0_02\bin>
Message was edited by:
aron_phil

