Skip to main content
gamedev.net
🔒 Locked

[java] Sorry.... but another input question

Started by Alpha_ProgDes Mar 24, 2004 at 3:57 PM 0 replies 350+ views
Original Post
Alpha_ProgDes
Alpha_ProgDes
I have no idea what is going on. It seems like when i use stndin.read() to read in a character that character just stays in the buffer stream and affects all if-elses. And I''ve looked in the Java source code and online and can''t find an inputbuffer flush. Here''s the culprit:
public void menu (NodeQueue StockQueue) throws IOException {
        
        boolean endLoop = false;
        //stndin = new BufferedReader(new InputStreamReader(System.in));

        while (!endLoop) {
            System.out.flush();
            System.out.print ("Would you like to buy some stock today? ");
            char answer = (char)stndin.read();
            if (answer == ''y'' || answer == ''Y'') {
                 buyStock(StockQueue);
                 System.out.println();    
            }
                          
            
            else if (answer == ''n'' || answer == ''N'') {
                if (!StockQueue.isEmpty()) {
                    System.out.print ("Would you like to sell your stock today? ");
                    answer = (char)System.in.read();
                    //before i used (char)stndin.read(), but it

// kept bypassing the if statement and execute the else

// statement if "answer == ''n''" from the prior read()

                    if (answer == ''y'' || answer == ''Y'')
                        sellStock(StockQueue);
                    else {
                        System.out.println();
                        System.out.println ("Program will now terminate. Thank you. ");
                        endLoop = true;
                    }
                }
            }
        }
        
    }
now here''s are the functions that are called:
public void sellStock (NodeQueue StockQueue) throws IOException {
        
        String line = null;
        int sold_shares = 0;
        
        System.out.println ("Please enter number of shares you wish to sell: ");
        
        line = stndin.readLine();
        sold_shares = Integer.parseInt(line);
        int s_price =  10 + (int)(Math.random() * 6);
        boolean isSharesThere = (sold_shares > 0);
        System.out.print (line);
        
        Stock boughtStock = (Stock)StockQueue.front();
        while (isSharesThere && !StockQueue.isEmpty()) {
           
            if (sold_shares > boughtStock.getShares()) {
                sold_shares = sold_shares - boughtStock.getShares();
                capital_gain += boughtStock.getShares()*(s_price - boughtStock.getPrices());
                StockQueue.dequeue();
            }
            else {
                int new_shares = boughtStock.getShares() - sold_shares;
                capital_gain += sold_shares*(s_price - boughtStock.getPrices());
                ((Stock)(StockQueue.front())).setShares(new_shares);
            }  
        }
and....
public void buyStock (NodeQueue StockQueue) throws IOException {
        /*BufferedReader*/ 
       // if (stndin == null)

          stndin = new BufferedReader(new InputStreamReader(System.in));
        
        String line = null;
        int new_shares = 0;
        System.out.print("Please enter number of shares you wish to buy: ");
        
        line = stndin.readLine();
        new_shares = Integer.parseInt(line);
        int r_price =  10 + (int)(Math.random() * 6);
        
        Stock newStock = new Stock(new_shares, r_price);
        StockQueue.enqueue(newStock);
        
    }
does anyone know how to flush the inputbuffer or just force it to take another input when prompted to?
Beginner in Game Development?  Read here. And read here.  
Alpha_ProgDes
Alpha_ProgDes
i figured out that i had to use System.in.read().
it had to do with pressing Enter... the web site didn''t give much detail.
Beginner in Game Development?  Read here. And read here.  

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.