我正在創建一個證券交易所類型的程序,到目前爲止我的輸入工作正常,因此它正確地接受了用戶的命令。然而,它對輸入的操作並不像預期的那樣工作。我感到困惑的第一件事就是爲什麼當我運行代碼時拋出一個NullPointerException異常。NullPointerException異常被拋出,程序沒有按預期終止
基本上,程序將接受3件事情的輸入,然後是它們之間的空白。因此,例如我想在每$ 20買30股,我會鍵入輸入類似如下:
b 30 20
它將然後將其分成3個部分,並將其存儲到一個數組。之後,它會比較數組的第一個索引,看看程序應該做什麼,在這個例子中,它會購買,所以它會調用buy方法並將股票數量和股票價值存儲到我的CircularArrayQueue中。
它獲取存儲在Node中的共享值和共享量,但是當我嘗試使用我的CirularArrayQueue調用enqueue方法來將節點存儲到隊列中時,它給了我一個NullPointerException。
我一直在遇到的另一個問題是程序的終止。當程序看到輸入的第一個索引值是「q」時,該程序應該終止。我做了一個while循環,聲明它會在布爾值quit爲false時循環。然後在while循環中,我做了一個if語句來檢查stockParts [0]的值是否爲「q」。如果是這樣,它會將quit的值更改爲true,以便可以結束循環,但由於某種原因,它不會終止,並且它仍在循環中。
我一直在這些問題上撓了幾個小時,但我似乎無法找到問題的根源。有人可以幫助我嗎?以下是從我的主類的代碼和CircularArrayQueue類:
import java.util.Scanner;
import java.lang.Integer;
public class StockTran {
String command = "";
String[] stockParts = null;
CircleArrayQueue Q;
boolean quit = false;
public StockTran(String inputCommand) {
try {
Scanner conReader = new Scanner(System.in);
this.command = inputCommand.toLowerCase();
this.stockParts = command.split("\\s"); // splits the input into three parts
buyShares(Integer.parseInt(stockParts[1]), Integer.parseInt(stockParts[2])); //testing purpose only
while (quit == false) {
if (this.stockParts[0] == "q") { // ends transaction and terminates program
System.out.println("Share trading successfully cancelled.");
quit = true;
}
if (this.stockParts == null || this.stockParts.length > 3) {
throw new Exception("Bad input.");
}
if (stockParts[0] == "b") { // checks to see if it is a buying of shares
int shares = Integer.parseInt(stockParts[1]); // stores share amount
int value = Integer.parseInt(stockParts[2]); // stores selling value
buyShares(shares, value); // calls buyShares method and adds share to queue
}
else if (stockParts[0] == "s") { // checks to see if it is a selling of shares
int shares = Integer.parseInt(stockParts[1]); // stores share amount
int value = Integer.parseInt(stockParts[2]); // stores selling value
sellShares(shares, value); // calls sellShares method
}
else if (stockParts[0] == "c") { // checks to see if it is capital gain
capitalGain(); // calls capitalGain and calculates net gain
}
System.out.println("Enter your next command or press 'q' to quit: ");
command = conReader.nextLine().toLowerCase();
stockParts = command.split("\\s");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void buyShares(int shareAmout, int shareValue) { // takes in share total and values for each share
Node temp = new Node(shareAmout, shareValue); // stores values into node
try {
Q.enqueue(temp); // enqueues the node into the CircularArrayQueue
//System.out.println(Q.toString());
} catch (FullQueueException e) {
e.printStackTrace();
}
}
public void sellShares(int shareAmount, int sharePrice) { // ToDo
}
public int capitalGain() { // ToDo
return 0;
}
public static void main(String[] args) {
String inputCommand = "";
Scanner mainReader = new Scanner(System.in);
System.out.println("Enter 'b' to purchase share, 's' to sell share, 'c' for capital gain, or 'Q' to quit: ");
inputCommand = mainReader.nextLine();
StockTran tran = new StockTran(inputCommand);
}
}
public class CircleArrayQueue implements Queue {
protected Node Q[]; // initializes an empty array for any element type
private int MAX_CAP = 0; // initializes the value for the maximum array capacity
private int f, r;
public CircleArrayQueue(int maxCap) {
MAX_CAP = maxCap;
Q = new Node[MAX_CAP]; // sets Q to be a specific maximum size specified
f = 0; // sets front value to be 0
r = 0; // sets rear value to be 0;
}
public int size() {
return (MAX_CAP - f + r) % MAX_CAP; // returns the size of the CircularArrayQueue
}
public boolean isEmpty() { // if front and rear are of equal value, Queue is empty
return f == r;
}
public Node front() throws EmptyQueueException { // method to get the front value of the CircularArrayQueue
if (isEmpty()) throw new EmptyQueueException("Queue is empty.");
return Q[f]; // returns object at front of CircularArrayQueue
}
public Node dequeue() throws EmptyQueueException { // method to remove from the front of the CircularArrayQueue
if (isEmpty()) throw new EmptyQueueException("Queue is empty.");
Node temp = Q[f]; // stores front object in local variable
Q[f] = null; // sets the value to be null in the array
f = (f + 1) % MAX_CAP; // sets the new front value to be this
return temp; // returns the object that was originally in the front
}
public void enqueue(Node element) throws FullQueueException { // method to add to the end of the CircualarArrayQueue
if (size() == MAX_CAP - 1) throw new FullQueueException("Queue has reached maximum capacity.");
Q[r] = element; // stores the new element at the rear of array
r = (r + 1) % MAX_CAP; // sets the new rear value to be the location after element insertion
}
}
噢好的。感謝您的幫助!現在我只需要弄清楚爲什麼程序不會終止...... – Asdeev
@MohamedBhura因爲if(this.stockParts [0] ==「q」)'永遠不會返回true。使用'equals()'而不是'=='應該修復它。 –
對於程序終止,請先嚐試修復字符串比較問題。 (順便說一句,歡迎來到StackOverflow。如果你認爲這是最好的答案,請點擊左邊的小複選框。) –