We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

looping problem

Hello everyone! I'm currently having a problem with my program it doesn't loop properly please help me with it. The code is below. Thanks in advance!

import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.*;
    public class Wewe{
        public static void main(String[]args){
            Scanner inp = new Scanner(System.in);
            boolean tryAgain;


            do{
            System.out.print("\nInput username: ");
            String user = inp.nextLine();
            System.out.print("\nInput password: ");
            String pass = inp.nextLine();

        if(user.equals("admin") && pass.equals("admin")){
                System.out.print("Success!");
                tryAgain = true;
            }
            if(user!="admin" && pass!="admin"){
                JOptionPane.showMessageDialog(null, "Try again! Invalid username or password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
            tryAgain = false;
        }
    }while(tryAgain = true);
            }
        }

What I want to happen is that once the user entered wrong username or password the program will then loop. But if the user entered the correct username or password, it wont loop asking the user for the correct one.

3
Contributors
4
Replies
20 Hours
Discussion Span
3 Months Ago
Last Updated
26
Views
angel06
Newbie Poster
24 posts since Aug 2012
Reputation Points: 12
Solved Threads: 0
Skill Endorsements: 0

while(tryAgain = true)

= is assignment, to test for equals use ==

but in this case, since tryAgain is already a boolean (true or false) yu can just say
while (tryAgain)

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

Hi sir I tried your suggestion but it gave me an error. It says vairable tryAgain might not have been initialized.

angel06
Newbie Poster
24 posts since Aug 2012
Reputation Points: 12
Solved Threads: 0
Skill Endorsements: 0

That's because the compiler has seen that the two if tests on lines 16 and 20 could both be false, in which case neither of the assignments on lines 18 and 22 will be executed, and the tryAgain variable will not have been initialised by you.
You need to decide what the correct value of tryAgain should be if both if tests fail, and initialise tryAgain to that value when you declare it, eg by changing line17 to boolean tryAgain = true;

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

@angel-do as @James has explained.
Either initialize variable tryAgain or add else statement for two if with the values of "tryAgain" in them.

IIM
Practically a Master Poster
653 posts since Jun 2011
Reputation Points: 127
Solved Threads: 140
Skill Endorsements: 8

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0737 seconds using 2.75MB