Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Alpha_ProgDes

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

This topic is 5298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!