for java programing topics:Integer.parseInt() methond, if, ligical operators ||
ID: 3673268 • Letter: F
Question
for java programing
topics:Integer.parseInt() methond, if, ligical operators || ("or") && ("and")
Code a program called AgeCheck.java to have the user input name and age. Output only the names of those people from the age of 21 throught and including 40 years old.
This is what i've done so far. I'm stuck on how to end the program once the user wants to quit and enters "q". Any help is appreciated. Thanks
.import javax.swing.*;
public class AgeCheck
{
public static void main (String[] args)
{
int age;
String name, enterAge;
do
{
name = JOptionPane.showInputDialog(null,
"Enter name or 'q' to quit");
enterAge = JOptionPane.showInputDialog(null,
"Enter age");
age = Integer.parseInt(enterAge);
if (age >= 21 && age <= 40)
{
JOptionPane.showMessageDialog(null, name);
}
else
break;
}while(name != "q");
}
}
Explanation / Answer
Actually when you write while(name !="q") the retruned vales is not 1 or 0 so its no ending.but now this method returns 0 if they are equal then it gets exit.
package agecheck;
import java.lang.*;
import javax.swing.*;
public class AgeCheck
{
public static void main (String[] args)
{
int age;
String name, enterAge;
do
{
name = JOptionPane.showInputDialog(null,
"Enter name or 'q' to quit");
enterAge = JOptionPane.showInputDialog(null,
"Enter age");
age = Integer.parseInt(enterAge);
if (age >= 21 && age <= 40)
{
JOptionPane.showMessageDialog(null, name);
}
else
break;
}while((name.compareTo("q"))!=0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.