java


Java do while loop with multiple conditions not executing properly


I have to write a Magic 8 ball program that will account for user input errors and I have to use a loop to do that.
boolean okay;
do {
System.out.printf("What is your question?\n");
questionStr = keyboard.nextLine();
int length = questionStr.length();
if (questionStr.length() == 0) {
System.out.println("Not allowed.");
okay = false;
} else if (!(questionStr.charAt(length - 1) == '?')) {
System.out.println("Add question mark.");
okay = false;
} else if (questionStr.length() > 60) {
okay = false;
}
okay = true;
} while (!okay);
When I run the code and make it an empty string, it does print out not allowed however it still runs the rest of the code and does not loop back and ask "What is your question?" The same happens with the question mark; it prints out "Add question mark" but does not loop back like it is supposed to. If I make a question longer than 60 characters, the code still executes and does not loop back and continue asking the user "What is your question?" until the code is less than 60 characters. I am trying to figure out what I am doing wrong here.
Move okay = true; before your if statements that negate it,
okay = true;
if (questionStr.length() == 0) {
System.out.println("Not allowed.");
okay = false;
} else if (!(questionStr.charAt(length - 1) == '?')) {
System.out.println("Add question mark.");
okay = false;
} else if (questionStr.length() > 60) {
okay = false;
}
As posted, you unconditionally set okay to true before your condition while (!okay); and thus the loop always ends.

Related Links

How to adjust Java AWT/Swing UI to system settings?
Special characters in filename java
Invalid Hadoop Runtime specified - Eclipse
Multiple widget instances - need to update each instance separately
Error when storing record into Google DataStore using AppEngine and EntityManager in Java
ivy doesn't download jar from remote after local
Cacerts compatibility with java versions
500 Internal server error django java
Javax.swing.text, Lowagie, HTMLWriter adding image (Not from file)
Subdividing executor services
When MongoDB DBRef lazyness is set to true, java.lang.IllegalArgumentException is throw
How to make JCheckBox from text file data
Changing course in Java properties
Java inheritance ArrayList type in method [duplicate]
For loop inside other for loop
Creating a monitor of messages between applications with Java

Categories

HOME
testing
ionic-framework
urbancode
view
stock
fft
fme
jsp-tags
bookshelf.js
at-command
yarn
twitter-bootstrap-4
retrofit
add
applepay
primary-key
wheelnav.js
etl
remote-access
leiningen
iron-router
percona
opentracing
pc
samoa
cloudhub
captiveportal
format-specifiers
icloud-api
blazemeter
tdd
sparse-matrix
url-scheme
libssl
c++-amp
http-get
wpf-controls
scaffold
event-driven
dynamics-crm-2013
galen
no-www
total-commander
.net-4.6.2
cubic-spline
rainbowtable
pebble-watch
brightcove
drupal-6
knockout-components
nodebb
flutterwave
account-kit
eventkit
ableton-live
fody
firebaseui
jwplayer7
disque
moveit
rhino-servicebus
icu4j
sankey-diagram
sortedlist
oxwall
winddk
websocket4net
project-planning
yorick
comexception
hyprlinkr
mysqltuner
dexterity
file-locking
interface-orientation
jquery-knob
chuck
venn-diagram
bubble-chart
qt-jambi
data-loss
spyware
deobfuscation
horizontal-accordion
netbeans-7.1
disclosure
propagation
mysql-error-1005
mozilla-prism
nhibernate.search
zend-tool
photoshop-cs4
scripting-languages
defensive-programming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App