Results 1 to 10 of 10
  1. #1
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default problem on expressing a boolean method

    hi guys,

    so i'm having a little problem with my program, and till now i cannot figure it out what it is.
    here is my program

    Java Code:
    import java.io.IOException;
    import java.util.Scanner;
    public class Punch {
        private static Scanner give=new Scanner(System.in);  
        public static boolean Sugar() throws IOException{
            char i;
            boolean sugar=true;
            i=(char) System.in.read();
            if(i=='y'){
                 sugar=true;
            }
            else if(i=='n'){
                 sugar=false;
                }
            return sugar;
        }
        public static String getSugar() throws IOException{
            boolean Sugar = Sugar();
            String sg = null;
            if(Sugar==false){
                sg="without sugar";
            }
            else {
                sg="with sugar";
            }
            return sg;
        }
        public static String getTemperature(){
           System.out.println("Which temperature?");
           String temp=give.next();
           System.out.println(temp);
           return temp;
        }
        public static int getFlavor() {
            int i = 0;
            i=give.nextInt();       
            return i;
        }
       
        public static void main(String[]args) throws IOException{
            String flavor;
            System.out.println("Choose flavor");
            System.out.println("[0] alcohol free punch"
                              +"\n[1] turbo punch (double Rum)"
                              +"\n[2] blood orange punch"
                              +"\n[3] apple punch with cinnamon");
            int fl=getFlavor();
            System.out.println("With sugar y/n?");
            boolean sugar =Sugar();
            String sugar1 = getSugar();
            String tp=getTemperature();
            System.out.println("Which amount");
            int amount=give.nextInt();
            if(fl==0){
                    flavor="alcohol free punch";
                    for(int i=0;i<amount;i++){
                    System.out.println(i+""+tp +""+flavor+""
                                +sugar1+"is ready for drinking");}}
            if(fl==1){
                        flavor="turbo punch (double Rum)";
                        for(int i=0;i<amount;i++){
                    System.out.println(i+""+flavor+""+fl+""
                                +sugar1+"is ready for drinking");}}
            if(fl==2){
                            flavor="blood orange punch";
                            for(int i=0;i<amount;i++){
                    System.out.println(i+""+flavor +""+fl+""
                                +sugar1+"is ready for drinking");}}
            if(fl==3){
                                flavor="apple punch with cinnamon";
                                for(int i=0;i<amount;i++){
                    System.out.println(i+""+flavor +""+fl+""
                                +sugar1+"is ready for drinking");}
                                }
                
                    }
                }
    the problem is with the boolean expression. I don't know why, but everytime i run the program, whatever i press when it comes to the question
    (With sugar y/n) the program will publish just "with sugar".....
    Can somebody tell me what i did wrong...??
    this is the output of the program
    Java Code:
    With sugar y/n?
    n
    Which temperature?
    hot
    Which amount
    3
    0 hot alcohol free punch with sugar is ready for drinking
    1 hot alcohol free punch with sugar is ready for drinking
    2 hot alcohol free punch with sugar is ready for drinking

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    The problem is not at all with the boolean method, but managing your input stream. I believe you were having some trouble with your Scanner in your Sugar() method, and that led you to try a whole bunch of things until you were quite confused.

    What's happening now is that you are calling your Sugar() method twice -- once directly from your main() method, and then again from within your getSugar() method. On the first call, the value is set correctly, but on the second call, it reads a \n character, and sets your boolean value to true. Remove the line:
    Java Code:
    		boolean sugar =Sugar();
    from your main() method, and your code will work. It still needs some cleaning up, though.
    Java Code:
    [COLOR="Red"]	public static boolean Sugar() throws IOException{
    		char i;
    		boolean sugar=true;
    		i=(char) System.in.read();
    		if(i=='y'){
    			sugar=true;
    		}
    		else if(i=='n'){
    			sugar=false;
    		}
    		return sugar;
    	}
    [/COLOR]
    Java Code:
    [COLOR="Blue"]	public static boolean sugar() throws IOException { // start method names with lower-case letters
    		String i; // use a String to go with nextLine();
    		boolean sugar = true; // consider removing this and throwing an Exception if you don't get a y or an n
    		i = give.nextLine();
    		if (i.equalsIgnoreCase("y")){
    			sugar = true;
    		}
    		else if (i.equalsIgnoreCase("n")) {
    			sugar = false;
    		}
    		return sugar;
    	}
    [/COLOR]
    In fact, you don't really need a separate method here -- why not fold this logic into getSugar()?

    -Gary-

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by b.m View Post
    Java Code:
        public static int getFlavor() {
            int i = 0;
            i=give.nextInt();       
            return i;
        }
    It may sound funny to you now but add a line give.nextLine() call just before you return your int value. The nexInt() method leaves the end-of-line character(s) in the input buffer and hinders your getSugar() method that is called next.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    thnx a lot man!

    I didn't notice that line at the main() method at all..:eek:

    Well actually i could fold it into getSugar(), but i'm not sure if it will work.
    Cuz i'm supposed to do this as a boolean method.....
    and in the beginning i tried to do that, but it just started to print just "true or false" in the end. And this is what i don't want...

  5. #5
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Originally Posted by b.m View Post
    Code:

    public static int getFlavor() {
    int i = 0;
    i=give.nextInt();
    return i;
    }

    It may sound funny to you now but add a line give.nextLine() call just before you return your int value. The nexInt() method leaves the end-of-line character(s) in the input buffer and hinders your getSugar() method that is called next.

    kind regards,

    Jos

    Sorry Jos, i don't really get your point..
    why should i add a line give.nextLine() there????:confused:

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by b.m View Post
    Sorry Jos, i don't really get your point..
    why should i add a line give.nextLine() there????:confused:
    I told you it would look funny to you; suppose you type '1' for a particular kind of flavor; what you actually typed is '1', <enter>. the <enter> is a character in the input buffer. The getFlavor() method uses the nextInt() method which removes the '1' from the input buffer and returns the value 1. But, it leaves the <enter> character in the input buffer. Because next you call the getSugar() method that simply reads a character from the input buffer it'll read the <enter> character. Because it's neither 'Y' or 'N' it assumes the user wants sugar (see your own method). You should get rid of that <enter> character after you've read an int so in your getFlavor() method you should read the entire rest of the line: give.readLine() and dump it. Try it and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Actually, Jos, that doesn't happen in this case, because he uses System.in.read() instead of his Scanner in his Sugar() method (probably because he was running into that very issue earlier).

    I personally think it's a rotten thing to introduce Scanner to first-year Java students, as it's a pain to learn, it's not terribly useful out in the real world (hardly anybody writes and runs console-based Java programs), and it makes the whole language look bad. Methods like nextInt() are especially useless as they're taught in these courses, as they assume that the user will actually type an int when asked for an int. This is a dangerous assumption. (Or else the student has to deal with InputMismatchException, and they're probably not ready for that.)

    If you must use Scanner, stick with the nextLine() method, and then parse the String. It's safer and saner.

    -Gary-

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    Actually, Jos, that doesn't happen in this case
    Yes it does: remove the 'boolean sugar= Sugar()' line and add a 'give.readLine()' method call to the getFlavor() method and see for yourself. All input scanning is a messy business whether you use a Scanner class or not ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    b.m
    b.m is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Hmmm....
    so u guys think that using this Scanner method, it's not good....But till now i have found it very useful, cuz it's easy to read any kind of input, except (char)...

    If u know a better way, please tell me...;)

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It's OK to use Scanner if you really want console input, but I would stick with the Scanner's .nextLine() method. Read a whole line into a String every time, and then parse that String into an int or a double or whatever if you need to. But you probably want a GUI or a web front-end anyway, so move in that direction.

    -Gary-

Similar Threads

  1. Boolean and Method help on Homework
    By gto400no1 in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:12 AM
  2. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 02:32 PM
  3. Testing boolean method
    By SteroidalPsycho in forum New To Java
    Replies: 7
    Last Post: 10-23-2009, 05:12 AM
  4. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 03:36 AM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •