?
Solved

System.in problem...

Posted on 2003-03-14
8
Medium Priority
?
507 Views
Last Modified: 2008-03-03
Hi, I am coding this BlackJack.java program, but I am not sure why the program runs "System.out.println("Game Continues, would you like to hit(H) or stand(S)?"); twice?  The first time it prints, it does not wait for Text.getlnString to catch the user input, then it prints the question again and it works fine.  I have added "*********PROBLEM AREA*********" to the line.  I have also added TextIO to the bottom of this page.

Thanks for any input,
noijet



/*
 * BlackJack.java
 *
 * Created on March 14, 2003, 9:42 AM
 */

package java_practice.chapter5;

import java_practice.chapter4.TextIO;

/**
 *
 * @author  gs10622
 */
public class BlackJack {
   
    private static BlackjackHand dealerHand;
    private static BlackjackHand playerHand;
    private static Deck deck;
    private static BlackjackPlayer player;
    private static boolean playAgain = true;
   
    /** Creates a new instance of BlackJack */
    public BlackJack() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        System.out.println("WELCOME TO AG CASINOS! Enjoy your game of Blackjack!");
        System.out.println();
       
        player = new BlackjackPlayer(100);
        System.out.println("How much do you want to bet?");
        double userBet = TextIO.getDouble();
        while(playAgain){
            playAgain = playBlackjack();
            if(playAgain == false){
                System.out.println("Are you sure you want to quit?  Hey, you never know.");
                playAgain = TextIO.getlnBoolean();
            }
        }
       
       
    }
   
    static boolean playBlackjack(){
        System.out.println("Blackjack game will begin now.");
        deck = new Deck(); //New deck
        deck.shuffle();
        dealerHand = new BlackjackHand();//new dealer hand
        playerHand = new BlackjackHand();//new player hand
        int numDealerCards = 0;
        int numPlayerCards = 0;
        //int dealerTotal = 0;
        //int playerTotal = 0;
        String userInput;
        Card hitCard;
       
        for(int i = 0; i < 2; i++){
            dealerHand.addCard(deck.dealCard());
            playerHand.addCard(deck.dealCard());
            numDealerCards++;
            numPlayerCards++;
        }

        //System.out.println("Dealer has " + dealerHand.getCardCount() + " cards");
        System.out.println("Dealer first card is " + dealerHand.getCard(0));
        //System.out.println("Dealer second card is " + dealerHand.getCard(1));        
        System.out.println("The dealer's hand value currently is: " + dealerHand.getBlackjackValue());
       
        //System.out.println("Player has " + playerHand.getCardCount() + " cards");
        System.out.println("Player first card is " + playerHand.getCard(0));
        System.out.println("Player second card is " + playerHand.getCard(1));
        System.out.println("The player's hand value currently is: " + playerHand.getBlackjackValue());
       
        if(dealerHand.getBlackjackValue() == 21){
            System.out.println("Dealer's hand value is 21, Dealer has won.");
            System.out.println("Would you like to play again? (y/n)");
            playAgain = TextIO.getlnBoolean();
            return playAgain;
        }
        else if(playerHand.getBlackjackValue() == 21){
            System.out.println("Player's hand value is 21, Player has won.");
            System.out.println("Would you like to play again? (y/n)");
            playAgain = TextIO.getlnBoolean();
            return playAgain;
        }
        else{
           
            while(true){
                System.out.println("Game Continues, would you like to hit(H) or stand(S)?");
//************************* PROBLEM AREA **********************************************************
                userInput = TextIO.getlnString();
                System.out.println("userInput = " + userInput);
                if(userInput.equalsIgnoreCase("h")){
                    System.out.println("Player hits.");
                    hitCard = deck.dealCard();
                    playerHand.addCard(hitCard);
                    System.out.println("Player gets " + hitCard);
                    if(playerHand.getBlackjackValue() > 21){
                        System.out.println("Player has busted with value: " + playerHand.getBlackjackValue());
                        System.out.println("Dealer has won, player has gone over 21.");
                        System.out.println("Would you like to play again? (y/n)");
                        playAgain = TextIO.getlnBoolean();
                        return playAgain;
                    }
                   
                       
                    System.out.println("Dealer hits.");
                    hitCard = deck.dealCard();
                    dealerHand.addCard(hitCard);
                    System.out.println("Dealer gets " + hitCard);

                    if(dealerHand.getBlackjackValue() == 21){
                        System.out.println("Dealer gets 21, dealer wins.");
                        System.out.println("Would you like to play again? (y/n)");
                        playAgain = TextIO.getlnBoolean();
                        return playAgain;
                    }
                    else if(dealerHand.getBlackjackValue() < 21 && playerHand.getBlackjackValue() == 21){
                        System.out.println("Player gets 21, player wins.");
                        System.out.println("Would you like to play again? (y/n)");
                        playAgain = TextIO.getlnBoolean();
                        return playAgain;
                    }

                       
                       
                   
                   
                }
                else if (userInput.equalsIgnoreCase("s")){
                    System.out.println("Player stands.");
                    /*
                    if(dealerHand.getBlackjackValue() >= playerHand.getBlackjackValue()){
                        System.out.println("Are you sure? Your hand is lower than dealers, dealer" +
                        " will now deal you a card.");
                        continue;
                    }*/
                    System.out.println("Player's hand value stands at " + playerHand.getBlackjackValue());
                   
                    //while(dealerHand.getBlackjackValue() <= playerHand.getBlackjackValue()){
                    while(dealerHand.getBlackjackValue() < 21){
                       
                        System.out.println("Dealer hits.");
                        hitCard = deck.dealCard();
                        dealerHand.addCard(hitCard);
                        System.out.println("Dealer gets " + hitCard);
                       
                       
                        if(dealerHand.getBlackjackValue() > 21){
                            System.out.println("Dealer has busted with value: " + dealerHand.getBlackjackValue());
                            System.out.println("Player has won, the dealer has gone over 21.");
                            System.out.println("Would you like to play again? (y/n)");
                            playAgain = TextIO.getlnBoolean();
                            return playAgain;
                        }
                       
                    }
                   
                    System.out.println("Dealer has won. Dealer's value of " + dealerHand.getBlackjackValue() +
                    " is larger than player's value of " + playerHand.getBlackjackValue());
                    System.out.println("Would you like to play again? (y/n)");
                    playAgain = TextIO.getlnBoolean();
                    return playAgain;
                }
               
             
            }
       
        }
       
        /*
        This commented section is replaced with getBlackjackValue
        for(int i = 0; i < numDealerCards; i++){
            dealerTotal = dealerTotal + dealerHand.getCard(i).getValue();
        }
        System.out.println(dealerTotal);
       
        System.out.println("Player has " + playerHand.getCardCount() + " cards");
        System.out.println("Player first card is " + playerHand.getCard(0));
        System.out.println("Player second card is " + playerHand.getCard(1));
        System.out.println("The player's hand value currently is: " + playerHand.getBlackjackValue());
        */
       
   
    }
   
}


******************** TEXTIO************************

/*
 * TextIO.java
 *
 * Created on February 18, 2003, 10:59 AM
 */

/**
 *
 * @author  gs10622
 */

/*
    The file defines a class TextIO, which provides a simple interface
    to Java's standard console input and output.  This class defines
    several static methods for reading and writing
    values of various type.
   
    This class will only work with standard, interactive applications.
    When it is used in such an application, System.out and System.in
    should not be used directly, since the TextIO class thinks it has
    exclusive control of System.out and System.in.  (Actually, using
    System.out will probably not cause any problems, but don't use
    System.in.)

    To use this class in your program, simply include the compiled class
    file TextIO.class in the same directory with the class file for your
    main program.  (If you are using a development environment such as
    CodeWarrior or Visual J++, you can include the source file,
    TextIO.java in your project.)  You can then use all the public static methods
    from the TextIO class in your program.  (In your programs, the names
    of the methods must be prefaced with "TextIO."  For example, you should
    use the name TextIO.getln() rather than simply getln().)
   
   
    Modified February, 2000; getChar() now skips blanks and CR's, and getAnyChar()
    can be used to read the next char even if it's a blank or CR.

*/

package java_practice;
import java.io.*;
   
public class TextIO {

   // *************************** I/O Methods *********************************
   
         // Methods for writing the primitive types, plus type String,
         // to the console, with no extra spaces.
         //
         // Note that the real-number data types, float
         // and double, a rounded version is output that will
         // use at most 10 or 11 characters.  If you want to
         // output a real number with full accuracy, use
         // "TextIO.put(String.valueOf(x))", for example.
         
   public static void put(int x)     { put(x,0); }   // Note: also handles byte and short!
   public static void put(long x)    { put(x,0); }
   public static void put(double x)  { put(x,0); }   // Also handles float.
   public static void put(char x)    { put(x,0); }
   public static void put(boolean x) { put(x,0); }
   public static void put(String x)  { put(x,0); }


         // Methods for writing the primitive types, plus type String,
         // to the console,followed by a carriage return, with
         // no extra spaces.

   public static void putln(int x)      { put(x,0); newLine(); }  // Note: also handles byte and short!
   public static void putln(long x)     { put(x,0); newLine(); }
   public static void putln(double x)   { put(x,0); newLine(); }  // Also handles float.
   public static void putln(char x)     { put(x,0); newLine(); }
   public static void putln(boolean x)  { put(x,0); newLine(); }
   public static void putln(String x)   { put(x,0); newLine(); }
 

         // Methods for writing the primitive types, plus type String,
         // to the console, with a minimum field width of w,
         // and followed by a carriage  return.
         // If output value is less than w characters, it is padded
         // with extra spaces in front of the value.

   public static void putln(int x, int w)     { put(x,w); newLine(); }   // Note: also handles byte and short!
   public static void putln(long x, int w)    { put(x,w); newLine(); }
   public static void putln(double x, int w)  { put(x,w); newLine(); }   // Also handles float.
   public static void putln(char x, int w)    { put(x,w); newLine(); }
   public static void putln(boolean x, int w) { put(x,w); newLine(); }
   public static void putln(String x, int w)  { put(x,w); newLine(); }


          // Method for outputting a carriage return

   public static void putln() { newLine(); }
   

         // Methods for writing the primitive types, plus type String,
         // to the console, with minimum field width w.
   
   public static void put(int x, int w)     { dumpString(String.valueOf(x), w); }   // Note: also handles byte and short!
   public static void put(long x, int w)    { dumpString(String.valueOf(x), w); }
   public static void put(double x, int w)  { dumpString(realToString(x), w); }     // Also handles float.
   public static void put(char x, int w)    { dumpString(String.valueOf(x), w); }
   public static void put(boolean x, int w) { dumpString(String.valueOf(x), w); }
   public static void put(String x, int w)  { dumpString(x, w); }
   
   
         // Methods for reading in the primitive types, plus "words" and "lines".
         // The "getln..." methods discard any extra input, up to and including
         //    the next carriage return.
         // A "word" read by getlnWord() is any sequence of non-blank characters.
         // A "line" read by getlnString() or getln() is everything up to next CR;
         //    the carriage return is not part of the returned value, but it is
         //    read and discarded.
         // Note that all input methods except getAnyChar(), peek(), the ones for lines
         //    skip past any blanks and carriage returns to find a non-blank value.
         // getln() can return an empty string; getChar() and getlnChar() can
         //    return a space or a linefeed ('\n') character.
         // peek() allows you to look at the next character in input, without
         //    removing it from the input stream.  (Note that using this
         //    routine might force the user to enter a line, in order to
         //    check what the next character is.)
         // Acceptable boolean values are the "words": true, false, t, f, yes,
         //    no, y, n, 0, or 1;  uppercase letters are OK.
         // None of these can produce an error; if an error is found in input,
         //    the user is forced to re-enter.
         // Available input routines are:
         //
         //            getByte()      getlnByte()    getShort()     getlnShort()
         //            getInt()       getlnInt()     getLong()      getlnLong()
         //            getFloat()     getlnFloat()   getDouble()    getlnDouble()
         //            getChar()      getlnChar()    peek()         getAnyChar()
         //            getWord()      getlnWord()    getln()        getString()    getlnString()
         //
         // (getlnString is the same as getln and is only provided for consistency.)
   
   public static byte getlnByte()       { byte x=getByte();       emptyBuffer();  return x; }
   public static short getlnShort()     { short x=getShort();     emptyBuffer();  return x; }
   public static int getlnInt()         { int x=getInt();         emptyBuffer();  return x; }
   public static long getlnLong()       { long x=getLong();       emptyBuffer();  return x; }
   public static float getlnFloat()     { float x=getFloat();     emptyBuffer();  return x; }
   public static double getlnDouble()   { double x=getDouble();   emptyBuffer();  return x; }
   public static char getlnChar()       { char x=getChar();       emptyBuffer();  return x; }
   public static boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer();  return x; }
   public static String getlnWord()     { String x=getWord();     emptyBuffer();  return x; }
   public static String getlnString()   { return getln(); }  // same as getln()
   public static String getln() {
      StringBuffer s = new StringBuffer(100);
      char ch = readChar();
      while (ch != '\n') {
         s.append(ch);
         ch = readChar();
      }
      return s.toString();
   }
   
   
   public static byte getByte()   { return (byte)readInteger(-128L,127L); }
   public static short getShort() { return (short)readInteger(-32768L,32767L); }  
   public static int getInt()     { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
   public static long getLong()   { return readInteger(Long.MIN_VALUE, Long.MAX_VALUE); }
   
   public static char getAnyChar(){ return readChar(); }
   public static char peek()      { return lookChar(); }
   
   public static char getChar() {  // skip spaces & cr's, then return next char
      char ch = lookChar();
      while (ch == ' ' || ch == '\n') {
         readChar();
         if (ch == '\n')
            dumpString("? ",0);
         ch = lookChar();
      }
      return readChar();
   }

   public static float getFloat() {
      float x = 0.0F;
      while (true) {
         String str = readRealString();
         if (str.equals("")) {
             errorMessage("Illegal floating point input.",
                          "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
         }
         else {
            Float f = null;
            try { f = Float.valueOf(str); }
            catch (NumberFormatException e) {
               errorMessage("Illegal floating point input.",
                            "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
               continue;
            }
            if (f.isInfinite()) {
               errorMessage("Floating point input outside of legal range.",
                            "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
               continue;
            }
            x = f.floatValue();
            break;
         }
      }
      return x;
   }
   
   public static double getDouble() {
      double x = 0.0;
      while (true) {
         String str = readRealString();
         if (str.equals("")) {
             errorMessage("Illegal floating point input",
                          "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
         }
         else {
            Double f = null;
            try { f = Double.valueOf(str); }
            catch (NumberFormatException e) {
               errorMessage("Illegal floating point input",
                            "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
               continue;
            }
            if (f.isInfinite()) {
               errorMessage("Floating point input outside of legal range.",
                            "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
               continue;
            }
            x = f.doubleValue();
            break;
         }
      }
      return x;
   }
   
   public static String getWord() {
      char ch = lookChar();
      while (ch == ' ' || ch == '\n') {
         readChar();
         if (ch == '\n')
            dumpString("? ",0);
         ch = lookChar();
      }
      StringBuffer str = new StringBuffer(50);
      while (ch != ' ' && ch != '\n') {
         str.append(readChar());
         ch = lookChar();
      }
      return str.toString();
   }
   
   public static boolean getBoolean() {
      boolean ans = false;
      while (true) {
         String s = getWord();
         if ( s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") ||
                 s.equalsIgnoreCase("yes")  || s.equalsIgnoreCase("y") ||
                 s.equals("1") ) {
              ans = true;
              break;
          }
          else if ( s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") ||
                 s.equalsIgnoreCase("no")  || s.equalsIgnoreCase("n") ||
                 s.equals("0") ) {
              ans = false;
              break;
          }
          else
             errorMessage("Illegal boolean input value.",
                          "one of:  true, false, t, f, yes, no, y, n, 0, or 1");
      }
      return ans;
   }
   
   // ***************** Everything beyond this point is private *******************
   
   // ********************** Utility routines for input/output ********************

   private static InputStream in = System.in;    // rename standard input stream
   private static PrintStream out = System.out;  // rename standard output stream

   private static String buffer = null;  // one line read from input
   private static int pos = 0;           // position of next char in input line that has
                                         //      not yet been processed


   private static String readRealString() {   // read chars from input following syntax of real numbers
      StringBuffer s=new StringBuffer(50);
      char ch=lookChar();
      while (ch == ' ' || ch == '\n') {
          readChar();
          if (ch == '\n')
             dumpString("? ",0);
          ch = lookChar();
      }
      if (ch == '-' || ch == '+') {
          s.append(readChar());
          ch = lookChar();
          while (ch == ' ') {
             readChar();
             ch = lookChar();
          }
      }
      while (ch >= '0' && ch <= '9') {
          s.append(readChar());
          ch = lookChar();
      }
      if (ch == '.') {
         s.append(readChar());
         ch = lookChar();
         while (ch >= '0' && ch <= '9') {
             s.append(readChar());
             ch = lookChar();
         }
      }
      if (ch == 'E' || ch == 'e') {
         s.append(readChar());
         ch = lookChar();
         if (ch == '-' || ch == '+') {
             s.append(readChar());
             ch = lookChar();
         }
         while (ch >= '0' && ch <= '9') {
             s.append(readChar());
             ch = lookChar();
         }
      }
      return s.toString();
   }

   private static long readInteger(long min, long max) {  // read long integer, limited to specified range
      long x=0;
      while (true) {
         StringBuffer s=new StringBuffer(34);
         char ch=lookChar();
         while (ch == ' ' || ch == '\n') {
             readChar();
             if (ch == '\n')
                dumpString("? ",0);
             ch = lookChar();
         }
         if (ch == '-' || ch == '+') {
             s.append(readChar());
             ch = lookChar();
             while (ch == ' ') {
                readChar();
                ch = lookChar();
             }
         }
         while (ch >= '0' && ch <= '9') {
             s.append(readChar());
             ch = lookChar();
         }
         if (s.equals("")){
             errorMessage("Illegal integer input.",
                          "Integer in the range " + min + " to " + max);
         }
         else {
             String str = s.toString();
             try {
                x = Long.parseLong(str);
             }
             catch (NumberFormatException e) {
                errorMessage("Illegal integer input.",
                             "Integer in the range " + min + " to " + max);
                continue;
             }
             if (x < min || x > max) {
                errorMessage("Integer input outside of legal range.",
                             "Integer in the range " + min + " to " + max);
                continue;
             }
             break;
         }
      }
      return x;
   }
   
   private static String realToString(double x) {
         // Goal is to get a reasonable representation of x in at most
         // 10 characters, or 11 characters if x is negative.
      if (Double.isNaN(x))
         return "undefined";
      if (Double.isInfinite(x))
         if (x < 0)
            return "-INF";
         else
            return "INF";
      if (Math.abs(x) <= 5000000000.0 && Math.rint(x) == x)
         return String.valueOf( (long)x );
      String s = String.valueOf(x);
      if (s.length() <= 10)
         return s;
      boolean neg = false;
      if (x < 0) {
         neg = true;
         x = -x;
         s = String.valueOf(x);
      }
      if (x >= 0.00005 && x <= 50000000 && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) {  // trim x to 10 chars max
         s = round(s,10);
         s = trimZeros(s);
      }
      else if (x > 1) { // construct exponential form with positive exponent
          long power = (long)Math.floor(Math.log(x)/Math.log(10));
          String exp = "E" + power;
          int numlength = 10 - exp.length();
          x = x / Math.pow(10,power);
          s = String.valueOf(x);
          s = round(s,numlength);
          s = trimZeros(s);
          s += exp;
      }
      else { // constuct exponential form
          long power = (long)Math.ceil(-Math.log(x)/Math.log(10));
          String exp = "E-" + power;
          int numlength = 10 - exp.length();
          x = x * Math.pow(10,power);
          s = String.valueOf(x);
          s = round(s,numlength);
          s = trimZeros(s);
          s += exp;
      }
      if (neg)
         return "-" + s;
      else
         return s;
   }
   
   private static String trimZeros(String num) {  // used by realToString
     if (num.indexOf('.') >= 0 && num.charAt(num.length() - 1) == '0') {
        int i = num.length() - 1;
        while (num.charAt(i) == '0')
           i--;
        if (num.charAt(i) == '.')
           num = num.substring(0,i);
        else
           num = num.substring(0,i+1);
     }
     return num;
   }
   
   private static String round(String num, int length) {  // used by realToString
      if (num.indexOf('.') < 0)
         return num;
      if (num.length() <= length)
         return num;
      if (num.charAt(length) >= '5' && num.charAt(length) != '.') {
         char[] temp = new char[length+1];
         int ct = length;
         boolean rounding = true;
         for (int i = length-1; i >= 0; i--) {
            temp[ct] = num.charAt(i);
            if (rounding && temp[ct] != '.') {
               if (temp[ct] < '9') {
                  temp[ct]++;
                  rounding = false;
               }
               else
                  temp[ct] = '0';
            }
            ct--;
         }
         if (rounding) {
            temp[ct] = '1';
            ct--;
         }
         // ct is -1 or 0
         return new String(temp,ct+1,length-ct);
      }
      else
         return num.substring(0,length);
     
   }
   private static void dumpString(String str, int w) {   // output string to console
      for (int i=str.length(); i<w; i++)
         out.print(' ');
      for (int i=0; i<str.length(); i++)
         if ((int)str.charAt(i) >= 0x20 && (int)str.charAt(i) != 0x7F)  // no control chars or delete
            out.print(str.charAt(i));
         else if (str.charAt(i) == '\n' || str.charAt(i) == '\r')
            newLine();
   }
   
   private static void errorMessage(String message, String expecting) {
                  // inform user of error and force user to re-enter.
       newLine();
       dumpString("  *** Error in input: " + message + "\n", 0);
       dumpString("  *** Expecting: " + expecting + "\n", 0);
       dumpString("  *** Discarding Input: ", 0);
       if (lookChar() == '\n')
          dumpString("(end-of-line)\n\n",0);
       else {
          while (lookChar() != '\n')
             out.print(readChar());
          dumpString("\n\n",0);
       }
       dumpString("Please re-enter: ", 0);
       readChar();  // discard the end-of-line character
       short a = (short)1.2222;
   }

   private static char lookChar() {  // return next character from input
      if (buffer == null || pos > buffer.length())
         fillBuffer();
      if (pos == buffer.length())
         return '\n';
      return buffer.charAt(pos);
   }

   private static char readChar() {  // return and discard next character from input
      char ch = lookChar();
      pos++;
      return ch;
   }

   private static void newLine() {   // output a CR to console
      out.println();
      out.flush();
   }

   private static boolean possibleLinefeedPending = false;

   private static void fillBuffer() {    // Wait for user to type a line and press return,
                                         // and put the typed line into the buffer.
      StringBuffer b = new StringBuffer();
      out.flush();
      try {
         int ch = in.read();
         if (ch == '\n' && possibleLinefeedPending)
            ch = in.read();
         possibleLinefeedPending = false;
         while (ch != -1 && ch != '\n' && ch != '\r') {
             b.append((char)ch);
             ch = in.read();
         }
         possibleLinefeedPending = (ch == '\r');
         if (ch == -1) {
            System.out.println("\n*** Found an end-of-file while trying to read from standard input!");
            System.out.println("*** Maybe your Java system doesn't implement standard input?");
            System.out.println("*** Program will be terminated.\n");
            throw new RuntimeException("End-of-file on standard input.");
         }
      }
      catch (IOException e) {
         System.out.println("Unexpected system error on input.");
         System.out.println("Terminating program.");
         System.exit(1);
      }
      buffer = b.toString();
      pos = 0;
   }

   private static void emptyBuffer() {   // discard the rest of the current line of input
      buffer = null;
   }
   
   
} // end of class TextIO

0
Comment
Question by:gen228
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:wide_awake
ID: 8141161
It might be a problem with this:

  private static char lookChar() {  // return next character from input
     if (buffer == null || pos > buffer.length())
        fillBuffer();
     if (pos == buffer.length())
        return '\n';
     return buffer.charAt(pos);
  }

which returns a '\n' char for some reason when you're at the end of the buffer.  The buffer may initially be filled with an empty string (since there is no user input at the start), so you're getting the default '\n' right away.

This would short-circuit your while(true) loop in which you read H/S from the user, causing the prompt to be printed twice.
0
 

Expert Comment

by:noijet
ID: 8143223
Thanks wide awake !for taking the time to look at my code!  I want to send you points but how do I do that?

noijet

0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8145006
You're welcome :)  

I don't know how to assign points - I've never done it...  
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:wide_awake
ID: 8145200
It's actually a problem with getDouble().  stay tuned...
0
 
LVL 3

Expert Comment

by:wide_awake
ID: 8145207
1> System.out.println("How much do you want to bet?");
2> double userBet = TextIO.getDouble();


Change line 2 to

new> double userBet = TextIO.getlnDouble();


Then it'll work fine :)

-Mark.
0
 

Expert Comment

by:noijet
ID: 8145714
Thanks Mark!  My program works great now thanks to you!  

thanks again,
noijet
0
 
LVL 3

Accepted Solution

by:
wide_awake earned 200 total points
ID: 8147133
no problem.  If you want to assign points, I think you just have to log in as "gen228" and choose to accept an answer.

-Mark.
0
 

Author Comment

by:gen228
ID: 8151500
Thanks again Mark.

-noijet
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question