I'm using if else and writing down some codes suddenly happens the else said the branch is never used... but it should work.
The statement looks like this
JOptionPane.showConfirmDialog(null,full_name+"\n"+number_age+"\n"+gender+"\n"+address+
"\n"+birthday+"\n"+cell_no,"CHECK INFORMATION!",JOptionPane.YES_NO_OPTION);
if(true){
JOptionPane.showMessageDialog(null,"Thank you for Entering");
}else{
JOptionPane.showMessageDialog(null,"Please Retry");
}
Check JOptionPane.showConfirmDialog response and compare with JOptionPane.YES_NO_OPTION:
int answer = JOptionPane.showConfirmDialog(null,full_name+"\n"+number_age+"\n"+gender+"\n"+address+
"\n"+birthday+"\n"+cell_no,"CHECK INFORMATION!",JOptionPane.YES_NO_OPTION);
if (JOptionPane.YES_OPTION == answer)) {
JOptionPane.showMessageDialog(null,"Thank you for Entering");
} else {
JOptionPane.showMessageDialog(null,"Please Retry");
}
Related
I'm using while loop and inside am using two if statements.
while(cr.moveToNext()){
if(cr.getCount() > 0){
if((cr.getString(5).equals(username)) && cr.getString(6).equals(password)
&& cr.getString(14).equals("success")) {
String un = cr.getString(2);
String uc = cr.getString(1);
String rc = cr.getString(4);
......................
Toast.makeText(LoginActivity.this, "Successfully Logged In", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Dashboard.class);
startActivity(intent);
} else{
Toast.makeText(LoginActivity.this, "Invalid Login", Toast.LENGTH_SHORT).show();
}
}
}
In this scenario, if I passed wrong value "Invalid Login" is executing many times [as it is inside while loop] and If I pass correct values both successfully login and Invalid login message is showing. How to make if condition to be properly work in this type of scenario...
Assuming you want to check that everything is in order before starting the activities you can do the loop twice, like below.
I also used do while, so as not to skip the first result.
private void handleCursor(Cursor cr){
boolean everythingGood = true;
do{
if (cr.getCount() > 14) { // <- if you are using cr.getString(14)
//to get "Success" string, then you should
//check for that too
if (!checkSuccess(cr) )
everythingGood = false; }
}else {
//not enough columns received.. assumed login fail.
//do your failure workflow
everythingGood = false;
}
} while(cr.moveToNext() && everythingGood);
if(everythingGood){
//second loop (no need to check any more, just spawn the activities)
cr.moveToFirst();
do{
createActivities(cr);
}while(cr.moveToNext();
}else {
handleFailure();
}
}
I moved your check condition out to another function to make it easier to see the while loop
private boolean checkSuccess(Cursor cr){
return cr.getString(5).equals(username)) && cr.getString(6).equals(password)
&& cr.getString(14).equals("success");
}
you can use break; or you can use a boolean flag variable , at first keep it true, then enter the conditions if it is true and once entered make the flag variable false in side that condition.
Boolean isValid;
isValid = true;
while(cr.moveToNext())
{
if (cr.getCount() > 0)
{
if ((cr.getString(5).equals(username))
&& cr.getString(6).equals(password) && cr.getString(14).equals("success"))
{
if (isValid)
{
isValid=false;
String un = cr.getString(2);
}
}
else { .......... }
}
}
I have written a code which has a forEach loop and a while loop, and i am trying to make repeated requests until condition is satisfied using while loop. However, I am unable to break out of the while loop, as it gives the error "Break outside switch or loop". Also how can i go to the next user iteration in the forEach loop.
My code:
while(count>0){
usersClient.getUserUUIDList().forEach(user -> {
try{
LOG.info("USER STATUS for user {}",user.getId().toString());
Optional<ExternalUserStatus> userStatus= adminClientFactory.newUssClient().getStatusForUser(orgId, Constants.IMP_ENTITLEMENT,user.getId().toString());
System.out.println(userStatus.toString());
if(userStatus.isPresent()&&(userStatus.get().getState()=="activated")){
//break;
//continue;
}
}
catch(Exception ex){
LOG.info("Exception caught while getting userStatus");
}
});
count--;
Thread.sleep(1000 * 60 *1);
}
Instead of break or continue (which won't work as they're in the lambda block), set count to zero:
count = 0;
If you need the value of count after you exit the loop, use a boolean as an exit condition:
boolean done = false;
while(count>0){
for (UUID user : usersClient.getUserUUIDList()){
try{
LOG.info("USER STATUS for user {}",user.getId().toString());
Optional<ExternalUserStatus> userStatus=adminClientFactory.newUssClient().getStatusForUser(orgId,Constants.IMP_ENTITLEMENT,user.getId().toString());
System.out.println(userStatus.toString());
done = (userStatus.isPresent() && (userStatus.get().getState()=="activated"))
}
catch(Exception ex){
LOG.info("Exception caught while getting userStatus");
}
});
count--;
if (done) break;
Thread.sleep(1000*60*1);
}
From Bill Horvath's answer,
done = (userStatus.isPresent() && (userStatus.get().getState()=="activated"))
done always takes the result of last user from usersClient.getUserUUIDList()
So better we can use one more break if activated.
Add at the end of for loop
if(done)
break;
Finally:
boolean done = false;
while(count>0){
for (UUID user : usersClient.getUserUUIDList()){
try{
LOG.info("USER STATUS for user {}",user.getId().toString());
Optional<ExternalUserStatus> userStatus=adminClientFactory.newUssClient().getStatusForUser(orgId,Constants.IMP_ENTITLEMENT,user.getId().toString());
System.out.println(userStatus.toString());
done = (userStatus.isPresent() && (userStatus.get().getState()=="activated"))
if(done) break;//Add this
}
catch(Exception ex){
LOG.info("Exception caught while getting userStatus");
}
});
count--;
if (done) break;
Thread.sleep(1000*60*1);
}
if (jamholderku.equals("1")){
params.put("sesi1", jamholderku);
}else {
params.put("sesi1", jamholder);
}
if (jamholderku2.equals("1")){
params.put("sesi2", jamholderku2);
}else {
params.put("sesi2", jamholder2);
}
if (jamholderku3.equals("1")){
params.put("sesi3", jamholderku3);
}else {
params.put("sesi3", jamholder3);
Log.d("paramskutes", jamholder3);
}
if (jamholderku4.equals("1")){
params.put("sesi4", jamholderku4);
}else {
params.put("sesi4", jamholder4);
}
if (jamholderku2_1.equals("1")){
params.put("sesi2_1", jamholderku2_1);
}else {
params.put("sesi2_1", jamholder2_1);
}
if (jamholderku2_2.equals("1")){
params.put("sesi2_2", jamholderku2_2);
}else {
params.put("sesi2_2", jamholder2_2);
}
if (jamholderku2_3.equals("1")){
params.put("sesi3", jamholderku2_3);
}else {
params.put("sesi3", jamholder2_3);
}
if (jamholderku2_4.equals("1")){
params.put("sesi4", jamholderku2_4);
}else {
params.put("sesi4", jamholder2_4);
}
if (jamholderku3_1.equals("1")){
params.put("sesi2_1", jamholderku3_1);
}else {
params.put("sesi2_1", jamholder3_1);
}
if (jamholderku3_2.equals("1")){
params.put("sesi2_2", jamholderku3_2);
}else {
params.put("sesi2_2", jamholder3_2);
}
if (jamholderku3_3.equals("1")){
params.put("sesi3", jamholderku3_3);
}else {
params.put("sesi3", jamholder3_3);
}
if (jamholderku3_4.equals("1")){
params.put("sesi4", jamholderku3_4);
}else {
params.put("sesi4", jamholder3_4);
}
if (jamholderku4_1.equals("1")){
params.put("sesi2_1", jamholderku4_1);
}else {
params.put("sesi2_1", jamholder4_1);
}
if (jamholderku4_2.equals("1")){
params.put("sesi2_2", jamholderku4_2);
}else {
params.put("sesi2_2", jamholder4_2);
}
if (jamholderku4_3.equals("1")){
params.put("sesi3", jamholderku4_3);
}else {
params.put("sesi3", jamholder4_3);
}
if (jamholderku4_4.equals("1")){
params.put("sesi4", jamholderku4_4);
}else {
params.put("sesi4", jamholder4_4);
}
return params;
when the first if statement right it will not execute other if statement
EX:
if (jamholderku.equals("1)) == True
then it will execute params.put(blablabla)
but it won't execute other if statement like if (jamholderku2.equals("1"))
but if it executing if (jamholderku2.equals("1"))
it won't execute if (jamholderku3.equals("1")) even if it was true
can you please help me? because it deadline was due temorrow, i need to submit it or i get kicked from my team
Addition:
Example
let's say we using if else
if (jamholderku.equals("1")){
params.put("sesi1", jamholderku);
}else if (jamholderku.equals("0"){
params.put("sesi1", jamholder);
}else if (jamholderku2.equals("1"){
params.put("sesi2", jamholder2)
}
if the first if true, those else if won't be executed so my database will be null
and if i using nested statement
if (jamholderku.equals("1")){
if (jamholderku2.equals("1"){
params.put("sesi2", jamholderku2)
}
params.put("sesi1", jamholderku)
}
if user were selecting session 2 option then only jamholderku2 set to be 1, this make jamholderku false, so all code won't be executed
Edit:
I want to make a program, the program will check, upload, disabling thing.
Here's how it work:
User select date (Ex: 21-01-2020)
User select session, there is 4 session (session 1, session 2, session 3, session 4), and the user only can select 1 session, example user select (session 1)
so the program will upload selected date, and selected session to a server
and when the other user wants to use this program
the second user will need to do step 1 too, but the program will check in the selected date is there is a session that is used by another user
if there is a session that is used by another user, example the first user in date 21-01-2020 select session 1, so the second user when selecting date 21-01-2020, can't select session 1, but still can select session 2, 3, 4
and this program allows user to select multiple date and session
Maybe we should all get on the same understanding right now.
This code will check the if-statement and after finding the first TRUE it would stop all checks:
String str = "hello";
if(str.equals("hello"){
dosomething();
} else {
dosomethingelse();
}
So right here it will execute dosomething() and dosomethingelse() will not be executed.
You are giving us an example like this:
String str = "huhu";
if(str.equals("hello"){
dosomething_1();
} else {
dosomethingelse_1();
}
if(str.equals("huhu"){
dosomething_2();
} else {
dosomethingelse_2();
}
The if-statements are not nested. The program will check the first if and execute dosomethingelse_1() and after that check the second if and will execute dosomething_2();
Nested if-statement would be something like that:
String str = "hello";
if(str.equals("hello"){
dosomething();
} else {
if(str.equals("huhu"){
dosomething();
} else {
dosomethingelse();
}
}
And in this case the program would not reach the if(str.equals("huhu")) because it already executed the true one.
So basically we do not understand your question.
I am having a hard time how to return into specific variable or how to return without getting any error base on my program.
class Facebook {
public static void main(String[]args){
String user = JOptionPane.showInputDialog(null,"Enter Username: ");
String pass = JOptionPane.showInputDialog(null,"Enter Password: ");
if(user.equals("jas")&&(pass.equals("bsit"))){
JOptionPane.showMessageDialog(null,"Welcome "+ user);
Selection Class = new Selection();
Selection.Selection1();
}
else if (!user.equals("jas")||(!pass.equals("bsit"))) {
JOptionPane.showMessageDialog(null,
"Invalid Username or Password",
"Wrong Authentication",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
class Selection{
public Selection1(){
try{
String select = JOptionPane.showInputDialog("[1]Home\n[2]Profile\n[3]Logout");
int numbers = Integer.parseInt(select);
if (numbers == 1){
JOptionPane.showMessageDialog(null,"Mang Tani: Lumakas ang hanging amihan halos nilipad ang mga bubong ng mga bahay\n\nJessica Soho: Isang sikat na pagkain sa davao inubos ng kabataan \n\n Boying Remulla: Walang pasok dahil sa malakas na ulan\n#WalangPasok.");
return select;
}
else if (numbers == 2){
JOptionPane.showMessageDialog(null,"Name: Ralph Jasper \n\n Age: 17 \n\n Address: Tierra Nevada, General Trias, Cavite");
}
else if (numbers == 3){
}
}
catch (NumberFormatException nfe){
JOptionPane.showMessageDialog(null,"Please input only numbers","Invalid Input",JOptionPane.ERROR_MESSAGE);
}
}
}
1) you want a method with a return value of a string
Replace
public Selection1(){
with
public String select()
2) all "paths" of a non-void method must result in a return statement. This does not mean a return statement needs to be inside any if else's, but you do need to return something within the method.
Suggestion: declare a String result = ""; outside of the try catch, return it after, outside of the catch, and assign it to your JOptionPane value in between like result = JOptionPane...
3) I'm assuming you actually want that value that's returned?
Selection selector = new Selection();
String selected = selector.select();
// TODO use that value
Notice: Java naming conventions -- methods are lowerCase, classes are UpperCase.
private void EnterbuttonActionPerformed(java.awt.event.ActionEvent evt) {
if (Usernamefield.getText().equalsIgnoreCase("User"));
if (Passwordfield.getText().equalsIgnoreCase("420"));
{
JOptionPane.showMessageDialog(null, "Welcome, User!");
}
else
{
JOptionPane.showMessageDialog(null, "Wrong Username/Password!");
}
It says that an else statement cannot work without an if, there are clearly 2 if statements however. I would like to create a simple log-in GUI in Java.
change
if (Usernamefield.getText().equalsIgnoreCase("User"));
if (Passwordfield.getText().equalsIgnoreCase("420"));
to
if (Usernamefield.getText().equalsIgnoreCase("User") &&
Passwordfield.getText().equalsIgnoreCase("420"))
{
JOptionPane.showMessageDialog(null, "Welcome, User!");
}
else
{
JOptionPane.showMessageDialog(null, "Wrong Username/Password!");
}
You want both conditions to be true in order to accept the user.
And don't put a ; at the end of an if condition, since that makes it an empty if statement that does nothing (that's the reason your else had no matching if).
Try the below code.
private void EnterbuttonActionPerformed(java.awt.event.ActionEvent evt) {
if (Usernamefield.getText().equalsIgnoreCase("User") && Passwordfield.getText().equalsIgnoreCase("420"))
{
JOptionPane.showMessageDialog(null, "Welcome, User!");
}
else
{
JOptionPane.showMessageDialog(null, "Wrong Username/Password!");
}