Click to See Complete Forum and Search --> : Do..While - No termination.


Relco
10-23-2006, 08:48 PM
This program asks the user to input
Name
Age
Height
(or press 'q' to quit)

If details are age = 30-40 or height 180-195
Message: +Name is suspect

my problem is that when i type q
the program doesnt terminate
so i guess the problem is at the 'while' statement

can anyone tell me whats wrong?
Thanks






import java.util.*;

public class CrimeInvestigation
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

String name;
int age,height;


do
{
System.out.print("Name of suspect (press 'q' to quit): ");
name = sc.next();
System.out.print("Age: ");
age = sc.nextInt();
System.out.print("Height: ");
height = sc.nextInt();

if ((age >= 30 && age <= 40) || (height >= 180 && height <= 195))
{
System.out.print(name+" is a suspect");
}
else
{
System.out.print(name+" is not a suspect");
}
System.out.println("");
System.out.println("");

} while (name != 'q');


}

}

sae
10-23-2006, 10:12 PM
the term 'q' is wrong you would want q

you should also use ignore case


!name.equalsIgnoreCase("q")


personally I would have set this up to be a while/do loop. This way you test the criteria before you start going through the loop. This does require you to set the variable name to something other than "q" before you start the loop but then you don't perform all the functions if the input is "q" (i.e. if someone enters q they shouldn't also have to input a value for height and age)

Relco
10-23-2006, 10:50 PM
how do i want q ...
im asking the program to terminate when 'q' is entered.. so when while is true the loop continues...

so should be like this then?


} while (name != q);


and where do i insert the
!name.equalsIgnoreCase("q")

srry but havnt got acrossed these cases yet..
Thanks

i just want my program to terminate with the q value : (

Relco
10-24-2006, 06:45 AM
Ok figured it out!

Thanks alot!

sae
10-24-2006, 08:40 AM
excellent :D

agent_x91
10-30-2006, 04:01 AM
Glad you've figured it out. Just a note: you were attempting to compare a char and a String: sae's solution compares two Strings, which is what is necessary. I'm sure you can work out that it ignores capitals from the name of the method :)

coding_star
11-01-2006, 09:42 PM
Yeah,just like agent_x91 said,if your program permit user's mistake,it well be a good work.