Jump to content
  • Advertisement


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


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

This topic is 5380 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.print ("Would you like to buy some stock today? ");
            char answer = (char)stndin.read();
            if (answer == ''y'' || answer == ''Y'') {
            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'')
                    else {
                        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());
            else {
                int new_shares = boughtStock.getShares() - sold_shares;
                capital_gain += sold_shares*(s_price - boughtStock.getPrices());
public void buyStock (NodeQueue StockQueue) throws IOException {
       // 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);
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

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!