LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2017, 12:07 PM   #1
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS
Posts: 125

Rep: Reputation: 12
Java, Do-While Loop, Loop Won't Break When Cnodition is Met


Code:
public void setStats() {
        boolean done = false;
        int temp[] = new int[6];
        do {
            System.out.println("Rolling the dice...");
            for (int i = 0; i < 6; i++) {
                temp[i] = rollStat();
            }
            System.out.println("The numbers rolled:");
            for (int i = 0; i < 6; i++) {
                System.out.println(temp[i]);
            }

            System.out.println("Do you want to keep these?(Y or N)");
            Scanner in = new Scanner(System.in);
            String answer = in.next();

            if (answer == "Y" || answer == "y") {
                done = true;
            } else if (answer == "N" || answer == "n"){
                done = false;
            }

        } while (done == false);
    }
setStats is called from main(). rollStats is a simple RNG that gives a number between 3 and 18.

The issue is that if you answer yes to the question, "Y" or "y," it sets done to true but it doesn't break the loop. I've also tried throwing a break statement at it. No joy.
 
Old 06-16-2017, 12:19 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,160

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
> if you answer yes to the question, "Y" or "y," it sets done to true

I doubt that. You should try to debug. Plus, use 'equals':
Code:
    done= answer!=null && (answer.equals("Y") || answer.equals("y"));
 
Old 06-16-2017, 12:42 PM   #3
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS
Posts: 125

Original Poster
Rep: Reputation: 12
@NevemTeve,
Thank you! Using the correct operator/method fixed the issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In a for loop that operates on multiple servers, need both the options to enter the password or to skip to next server in loop rajkamalhm Linux - Newbie 7 06-08-2016 09:28 PM
break out oa while loop once size condition is met casperdaghost Linux - Newbie 6 02-24-2012 01:11 AM
How to break if loop in Perl? Barca Programming 9 08-03-2011 01:15 PM
break while [...] loop when a program finishes throughthegreens Linux - Newbie 6 02-18-2010 11:12 AM
break loop in C++ marek Programming 10 09-11-2003 10:59 AM


All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration