ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What don't you understand? Yes, Java has a data type called boolean that can store either true or false. You can simply ask the user the question and set your boolean variable to true or false depending on their answer.
Tell us specifically what you don't understand and post any attempt you've made at writing the code.
The first thing i don't understand is how to enter the question. I am assuming it's something like:
Quote:
h1 = keyboard.nextInt(boolvar);
boolean boolvar;
if (h1 == true) {System.out.println ("It is a weekned");}
else if (h1 == false) {System.out.println ("It is not a weekend");}
final JOptionPane optionPane = new JOptionPane(
"The only way to close this dialog is by\n"
+ "pressing one of the following buttons.\n"
+ "Do you understand?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
Nylex thanks you very much for your useful link you've been a great help so far in both this and my other thread.
Thank you Paulsm4 for your well commented example, very helpful.
And thank you to Proud for providing a nice alternative.
I'll go over everything you've all given me and try to understand it completely and i'll let you know if my program works. Thanks again!
Edit: Nylex, the book we're using is called "Java: An Introduction to Problem Solving and Programming (6th Edition)" but unfortunately i haven't received it yet from amazon.com. Screwed if i'm paying the price they're asking for it at the Uni bookshop.
Last edited by phoenixfire; 07-31-2011 at 10:41 PM.
Reason: Missed some info
Error: The operator && is undefined for the argument type(s) int, boolean
When trying to compile. As i think i'm getting really close i'll just post the entire question and my program.
Quote:
////////////////////////////// PROBLEM STATEMENT //////////////////////////////
// Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a //
// boolean indicating if we are on vacation, print a string of the form //
// "7:00" indicating when the alarm clock should ring. Weekdays, the alarm //
// should be "7:00" and on the weekend it should be "10:00". Unless we are //
// on vacation -- then on weekdays it should be "10:00" and weekends it //
// should be "off". //
// 1, false -> "7:00" //
// 1, false -> "7:00" //
// 1, false -> "7:00" //
///////////////////////////////////////////////////////////////////////////////
// >>>>>> Your Java Code Fragment starts here <<<<<<
Scanner keyboard = new Scanner (System.in);
int d1;
System.out.println("What day is it today? (0=sun, 1=mon, 5=fri, 6=sat, ect");
d1 = keyboard.nextInt( );
System.out.println("Is it a holiday today?");
Boolean h1 = keyboard.nextBoolean();
if (d1 >= 1 && d1<=5 && h1 == true) {System.out.println ("10:00");}
else if (d1 >= 1 && d1<=5 && h1==false) {System.out.println ("7:00");}
else if (d1 == 0||6 && h1==true) {System.out.println ("off");}
else if (d1 == 0||6 && h1==false) {System.out.println ("10:00");}
// >>>>>> Your Java Code Fragment ends here <<<<<<
Thanks for your reply but i have it working another way now.
Now i'm getting some kind of error.
For testing if we've completed the question or not they have given us a program that enters values into the program we've written and checks them against what the answer should be.
For the end result i've got
Quote:
Scanner keyboard = new Scanner (System.in);
int d1;
boolean h1;
//System.out.println("What day is it today? (0=sun, 1=mon, 5=fri, 6=sat, ect");
d1 = keyboard.nextInt( );
//System.out.println("Is it a holiday today?");
h1 = keyboard.nextBoolean();
if (d1 >= 1 && d1<=5 && h1 == true) {System.out.println ("10:00");}
else if (d1 >= 1 && d1<=5 && h1==false) {System.out.println ("7:00");}
else if (d1 <= 0 && d1 >=6 && h1==true) {System.out.println ("off");}
else if (d1 <= 6 && d1 >=6 && h1==false) {System.out.println ("10:00");}
With the questions commented out so that the program will only read the "10:00" ect.
The strange thing is it will run fine if i don't have them commented out but won't pass since the output also includes my 'please enter' questions but when they are commented out i get
Quote:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
Sounds like the program is reading all the input from your program. One (very ugly) workaround might be to print the questions to stderr instead of stdout.
I'd imagine your error may well be buffer related (ie, not being flushed) but I wouldn't like to say for definite as I've only had a 2 second glance at your code.
Firstly I think you're now aware that d1 == 0||6 isn't the correct shorthand for d1 == 0 || d1 == 6, but it also isn't equivalent to the latter logically.
d1 <= 6 && d1 >=6 is equal to d1 == 6, which mean d1 == 0 isn't tested. I think you meant d1 <= 0 && d1 >=6
As to why you're program behaves differently when you're not interleaving the out.printlns with the keyboard.nextTypes, consider that you're using a Scanner class wrapping System.in. Check how it behaves around giving empty tokens, blocking(aka waiting without you writing specific code to make it) differently when calling next() vs hasNext() (maybe you should wait while( !hasNext() ) ?), and that it may determine the end of the input has been reached before you want it to.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.