2011-08-21 163 views
3

我剛剛學過java,但是從我從C++學來的舊經驗,我想我可以編寫一個命令行計算器,它只需一行支持所有4個基本操作符。但我有一些問題。CommandLine Java計算器

這是我的代碼:

import java.util.Scanner; 

public class omg { 
    public static void main(String args[]) { 
     int fnum,snum,anum = 0; 
     String strtype; //The original calculation as string 
     char[] testchar; //Calculation as chararray 
     char currentchar; //current char in the char array for the loop 
     int machinecode = 0; //Operator converted to integer manually 
     String tempnumstr; //first and second numbers temp str to be converted int 
     int operatorloc = 0; //operator location when found 
     char[] tempnum = new char[256]; 
     Scanner scan = new Scanner(System.in); // The scanner obviously 
     System.out.println("Enter The Calculation: "); 
     strtype = scan.nextLine(); 
     testchar = strtype.toCharArray(); //converting to char array 
     for(int b = 0; b < testchar.length; b++) //operator locating 
     { 
      currentchar = testchar[b]; 
      if(currentchar == '+') { 
       machinecode = 1; 
       operatorloc = b; 
      } 
      else if(currentchar == '-') { 
       machinecode = 2; 
       operatorloc = b; 
      } 
      else if(currentchar == '*') { 
       machinecode = 3; 
       operatorloc = b; 
      } 
      else if(currentchar == '/') { 
       machinecode = 4; 
       operatorloc = b; 
      } 
     } 
     for(int t = 0;t < operatorloc;t++) { //transferring the left side to char 
      tempnum[t] = testchar[t]; 
     } 
      tempnumstr = tempnum.toString(); //converting char to string 
      fnum = Integer.parseInt(tempnumstr); //parsing the string to a int 
     for(int temp = operatorloc;temp < testchar.length;temp++) { //right side 
      for(int t = 0;t<(testchar.length-operatorloc);t++) { 
       tempnum[t] = testchar[temp]; 
      } 
     } 
     tempnumstr = tempnum.toString(); //converting to char 
     snum = Integer.parseInt(tempnumstr); //converting to int 
     switch(machinecode) { //checking the math to be done 
     case 1: 
      anum = fnum + snum; 
      break; 
     case 2: 
      anum = fnum - snum; 
      break; 
     case 3: 
      anum = fnum * snum; 
      break; 
     case 4: 
      anum = fnum/snum; 
     } 
     System.out.println(anum); //printing the result 
    } 
} 

這是我的代碼,但是當我運行它,它會問我的計算,然後給這個錯誤。

Exception in thread "main" java.lang.NullPointerException 
    at omg.main(omg.java:38) 

有可能是一個更好,更容易的方式做到這一點,我想聽到一個更好的方式和我的方式修復。提前

+2

你的C(++)的習慣是可見的。 Java中的類應以大寫字母開頭。變量中的每個單詞也應以大寫字母開頭(例如:machineCode)。變量通常在使用時聲明和初始化。並非所有在方法的開始。 machineCode應該是一個枚舉而不是int。 –

+1

只有在你的第二篇文章中有正確代碼縮進的格式化代碼才能使用! –

回答

6

氣墊船Full Of Eels已經向你指出了NullPointerException.的原因除此之外,我看到了很多可以在你的代碼中改進的東西。以下是我想做到這一點:

import java.util.Scanner; 

public class SimpleCalculator { 

    public static void main(String[] args) { 
     System.out.println("Please enter your calculation"); 
     Scanner scanner = new Scanner(System.in); 
     int left = scanner.nextInt(); 
     String op = scanner.next(); 
     int right = scanner.nextInt(); 
     System.out.println(compute(left, op, right)); 
    } 

    private static int compute(int left, String op, int right) { 
     switch (op.charAt(0)) { 
     case '+': 
      return left + right; 
     case '-': 
      return left - right; 
     case '*': 
      return left * right; 
     case '/': 
      return left/right; 
     } 
     throw new IllegalArgumentException("Unknown operator:" + op); 
    } 
} 

注意,掃描儀假定有空格前後的操作後。

輸出示例:

Please enter your calculation 
1 + 2 
3 

詳細的改進:

  1. 變量可以聲明,其中首先使用它們。在Java中習慣使用(更短的代碼長度,不需要重複變量名稱)。
  2. Scanner除了讀取整行外,還提供了標記。沒有必要重新發明輪子。
  3. char(是一個整數類型)可以是switch編輯。
+2

尼斯1+。只是我不想提及的一個小小建議:與它們一起處置資源總是一個好習慣,這包括Scanner對象,在您使用它時應該關閉它。這真的不會對這個計劃有所幫助,這就是爲什麼我討厭提及它的原因,但這是一個很好的習慣,因爲有時候它會影響它。 :) –

+0

你的方式是令人難以置信的更好,但我仍然在學習java :)。我只是想嘗試這種方式。 – Learath2

7

感謝您聲明:

char[] tempnum = null; 

但是,你在哪裏設置=一個非空值?因此,無論何時您嘗試使用它,就像它是一個完全啓動的物體一樣,您將得到一個NPE拋出。

編輯:你的代碼還有其他問題,包括調用數組中的toString(),它將返回數組的默認值toString - 不是你想要的那種情況。

所以,與其這樣:

tempnumstr = tempnum.toString(); 

你可能想是這樣的:

tempnumstr = new String(tempnum); 

或可能

tempnumstr = new String(tempnum).trim(); // get rid of trailing whitespace if needed 

編輯2:你似乎有兩個字符數組中你的程序,tempnum和testchar,一個填充字符和一個沒有。他們兩個的目的是什麼?考慮用一些評論加密你的代碼,這樣我們可以更好地理解它,並且能夠更好地幫助你。

+0

我修復了NPE並評論了我的代碼,但現在我在'java.lang.Integer.parseInt(Unknown Source)' – Learath2

+0

@ Leararth2:您遇到了新問題,我們需要查看您的更新代碼。由於這與原始問題無關並且保持清潔,所以最好在SO上提出一個新問題。 –

0

在38行嘗試存取權限的變量tempnum被初始化爲null你必須初始化變量tempnum這樣的: tempnum = new char[n] 其中n是數組的長度

2

你的問題是這樣的線:

tempnum[t] = testchar[t];

如先前宣佈它爲空這將拋出一個NullPointerException:char[] tempnum = null;

您需要將其更改爲char[] tempnum = new char[size];,將其初始化爲空數組以保存類型char。其中size是任何整數。

1
char[] tempnum = null; 

應設置類似

char[] tempnum = new char[4]; 

基本上在38行使用時爲空。

0

您忘記分配tempNum,當您嘗試在數組上下文中使用它時,結果爲NUllPointerException

char[].toString()不會做你所期望的(它返回一個數組對象的散列碼),使用數組的內容創建一個字符串使用new String(char[])

0

首先,它錯誤在這行:tempnum[t] = testchar[t]; 原因:tempnum沒有指向任何東西(空) 修復:tempnum = testchar;tempnum = new char[testchar.length]