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

[2624 byte] By [aron_phila] at [2007-11-27 9:59:26]
# 1

Recommendations

1) never use == or != to compare strings. Use equals() method instead.

do this:

if (string1.equals(string2))

{

}

not this:

if (string1 == string2)

{

}

For not equals use if (!string1.equals(string2))

2) Use code tags. Read on them here:

http://forum.java.sun.com/help.jspa?sec=formatting

3) To repeat something, use a loop. In this situation since you don't know how many times you will be looping (you'll go until the user gets it right), you'd use a "while" loop. Other times if you know you'll loop say 10 times, you'd use a "for" loop.

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:30:29 > top of Java-index,Java Essentials,New To Java...
# 2

Actually, I often use the equalsIgnoreCase instead of teh equals method, but you use them the same way.

Here are two examples:

import java.util.Scanner;

class Fubar

{

private Scanner sc = new Scanner(System.in);

private void testKeepGoing()

{

boolean keepGoing = true;

while (keepGoing)

{

System.out.print("Enter \"goodbye\" to quit: ");

String goodbyeString = sc.nextLine();

if (goodbyeString.equalsIgnoreCase("goodbye"))

{

keepGoing = false;

}

}

}

private void testTypeQuit()

{

Scanner sc = new Scanner(System.in);

String quitString = "";

while (!quitString.equalsIgnoreCase("quit"))

{

System.out.print("Type \"quit\" to exit: ");

quitString = sc.nextLine();

}

sc.close();

}

public static void main(String[] args)

{

Fubar foo = new Fubar();

foo.testKeepGoing();

foo.testTypeQuit();

}

}

petes1234a at 2007-7-13 0:30:29 > top of Java-index,Java Essentials,New To Java...
# 3
The "break b1;" statement doesn't look very nice - and I don't think it's doing what you think. Use a loop of some sort to ensure that there is valid input.
pbrockway2a at 2007-7-13 0:30:29 > top of Java-index,Java Essentials,New To Java...