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

extract multiple columns data from db and write to a file using spark?
Overriding value in heap java programming
Set keyboard focus
How to update a value in an arraylist<arraylist<Integer>>
Result gets written multliple times in the file
How to view Tomcat errors on netbeans
How can I separate a sentence including withe-space, digit and String?
Replacing an ImageView Image Correctly
Spring is not injecting a property
How to save image information got by “identify -verbose” command in a Hashtable Object programatically using java?
How can I manage feed from many different source? [closed]
Guice Injection While Using Reflection to Call Multiple Classes
Output of returning the digit does not come as expected?
String array argument error using java [duplicate]
JFrame - setLocation
Can't write List<object> in Parcel

Categories

HOME
ionic-framework
date
gitlab
vbscript
oracle11g
webpack-2
slick-slider
rascal
google-cloud-spanner
zend-framework3
size
orchardcms
fallback
flask-wtforms
windows-server-2012
clearcase-ucm
foselasticabundle
undefined
scichart
jtextfield
ml
jasonette
functional-testing
accessor
captiveportal
trading
sqlcipher
centos6.5
traffic
neo4j-spatial
objectanimator
google-cloud-nl
nouislider
fabric8
.net-4.0
winrt-xaml-toolkit
code-contracts
file-format
event-driven
theano.scan
gzipstream
ncalc
ibpy
mime
unixodbc
long-polling
rdfs
jxcore
carrot
slickedit
magma
r-forge
eventkit
yt-project
google-feed-api
spring-android
android-textview
qpid
wireshark-dissector
wyam
ibaction
microbenchmark
php-ci
javax.mail
facebook-graph-api-v2.4
knuth
fpml
ceil
htmlcleaner
transcoding
cloud-connect
stxxl
industrial
.aspxauth
dir
quartz-graphics
distutils
simba
sublist
google-email-migration
dice
work-stealing
xdomainrequest
coda-slider
infrastructure
bespin
perfect-hash
web-architecture
zend-tool
sector
spec#
msdev
signal-handling

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