1

In my code below, Scanner fails to work after more than one use of the method, and I can't figure out why.

public static void stockAccount(StockAccount uStock){
    System.out.println("\nWelcome to the Stock Account Management System");
    int input = 1;
    String[] options = {"Find the price of a stock", "Display your portfolio", 
            "Buy stock", "Sell stock", "View a graph of your portfolio value", "View your account history", "Exit"};
    String symbol;
    int shares;
    while(input != 7){
        input = NumberedMenu("Enter the operation you would like to access", options);
        if(input == 1){
             System.out.println("Enter the symbol of the stock you would like to find information on.");
             String str = a.nextLine();
             uStock.displayStockPrice(str);
        }
        else if(input == 2){
            uStock.display();
        }
        else if(input == 3){
            System.out.println("\t\tBUY A STOCK:\nEnter the stock symbol for the stock you would like to purchase");
            symbol = a.nextLine();
            if(uStock.makeStock(symbol) == null)
                System.out.println("Error: You did not enter a valid symbol. " + ERROR);
            else{
            System.out.println("Enter the number of shares you would like to buy");
            shares = a.nextInt();
            if(shares < 1){
                System.out.println("Error: You can only purchase a positive number of shares. " + ERROR);
                break;
            } System.out.println("Enter the max price you are willing to pay for this purchase");
            int max = a.nextInt();
            uStock.buy(symbol, shares, max);
            }
        }
        else if(input == 4){
            System.out.println("\t\tSELL SHARES:");
            System.out.println("Enter the symbol of the stock you would like to sell");
            symbol = a.nextLine();
            System.out.println("Enter the number of shares you want to sell");
            shares = a.nextInt();
            System.out.println("Enter the minimum price you want each stock to sell for");
            double price = a.nextDouble();
            uStock.sell(symbol, shares, price); 

        }

        else if(input == 5){
            System.out.println("Not currently implemented");
        }

        else if(input ==6){
            System.out.println("\t\tHISTORY:");
            for(String s : uStock.readFile("stock_transaction_history.txt"))
                System.out.println(s);
            System.out.println();
        }
        else if(input == 7){
            break;
        }
    }
}

The method NumberedMenu takes two parameters, a String and a String array and prints out the String as the headline with the array elements as options for the user to enter for the menu. This works every time this while loop runs, without error. But after the first iteration of the loop where the user enters an input and is moved to the appropriate condition, when the loop runs again and they enter their choice, the Scanner method that lets them enter their own value/string/whatever doesn't work and it skips directly to the next line of code. The output of running this code is below:

One moment please, while stock data is fetched from Yahoo! Finance...

Our records show no previous activity from this location. A new bank account and stock account are being made for you now.

Welcome to the Account Management System

Please select an account to access

  1. Stock Portfolio Account

  2. Bank Account

  3. Exit

Your Choice: 1

Welcome to the Stock Account Management System

Enter the operation you would like to access

  1. Find the price of a stock

  2. Display your portfolio

  3. Buy stock

  4. Sell stock

  5. View a graph of your portfolio value

  6. View your account history

  7. Exit

Your Choice: 1

Enter the symbol of the stock you would like to find information on.

T

Current price for T: $34.57

Enter the operation you would like to access

  1. Find the price of a stock

  2. Display your portfolio

  3. Buy stock

  4. Sell stock

  5. View a graph of your portfolio value

  6. View your account history

  7. Exit

Your Choice: 3

    BUY A STOCK:

Enter the stock symbol for the stock you would like to purchase

T

Enter the number of shares you would like to buy

4

Enter the max price you are willing to pay for this purchase

900

Sucessfully purchased 4 shares of T for $138.28.

Enter the operation you would like to access

  1. Find the price of a stock

  2. Display your portfolio

  3. Buy stock

  4. Sell stock

  5. View a graph of your portfolio value

  6. View your account history

  7. Exit

Your Choice: 3

    BUY A STOCK:

Enter the stock symbol for the stock you would like to purchase

Error: You did not enter a valid stock symbol.

Error: You did not enter a valid symbol. Operation aborted.

Enter the operation you would like to access

  1. Find the price of a stock

  2. Display your portfolio

  3. Buy stock

  4. Sell stock

  5. View a graph of your portfolio value

  6. View your account history

  7. Exit

Your Choice:

Notice that after purchasing the stock, once the method is called again and it goes to the same condition, it skips the Scanner method entirely. I don't know why, but it's probably something I did stupidly.

RedText
  • 21
  • 1
  • 4

0 Answers0