Results 1 to 10 of 10
- 12-12-2010, 01:38 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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
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 questionJava 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");} } } }
(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
- 12-12-2010, 02:25 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
from your main() method, and your code will work. It still needs some cleaning up, though.Java Code:boolean sugar =Sugar();
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]In fact, you don't really need a separate method here -- why not fold this logic into getSugar()?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]
-Gary-
- 12-12-2010, 03:23 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2010, 03:26 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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...
- 12-12-2010, 03:30 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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:
- 12-12-2010, 03:53 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2010, 04:14 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 12-12-2010, 04:42 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2010, 06:10 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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...;)
- 12-12-2010, 06:17 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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
-
Boolean and Method help on Homework
By gto400no1 in forum New To JavaReplies: 3Last Post: 02-22-2010, 12:12 AM -
Boolean method help
By syferite in forum New To JavaReplies: 6Last Post: 10-28-2009, 01:32 PM -
Testing boolean method
By SteroidalPsycho in forum New To JavaReplies: 7Last Post: 10-23-2009, 04:12 AM -
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks