2014-03-12 57 views
0

我有做一個java命令的程序,我有一些代碼,但我不知道如何繼續我停留在一個地方,順便說一句,我想使命令有點問題/sendcash [username] [money] // how it looks like製作輸入命令

我有驗證碼:

if (cmd.equals(AdminCommands[1])) { 
     String player = scanner.next(); 
     int money = scanner.nextInt(); 
     File folder = new File(player); 
     File pFile = new File(folder, player + ".txt"); 
     File bFile = new File(folder, money + ".txt"); 
     if (pFile.exists() && bFile.exists()) { 
      try { 
       Account pAcc = new Account(player, money); 
       if(pAcc.admin != 1) { 
        try { 
         writer = new BufferedWriter(new FileWriter(bFile)); 
         writer.write(player); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } finally { 
         try { 
          writer.close(); 
         } catch (Exception e) { 
         } 
        } 
        LabelInfo.setText("Money transfer complited ! (" + money + ") to (" + pAcc.name + ")"); 
       } else { 
        LabelInfo.setText("You can't transfer money to an admin!"); 
       } 
      } catch (Exception e) { 
       JOptionPane.showMessageDialog(null, "Username doesn't exist!"); 
      } 
     } 
    } 

編輯現在有了這個代碼沒有在控制檯中的文件太多我不知道這裏做什麼發生在類的代碼Account

public Account(String player, int cash) { 
    this.username = player; 
    this.money = cash; 
} 

回答

0

問題解決了我實際上已經在構造類Account錯過了一些東西,也有人可以給我解釋一下爲什麼在writer.write(""+cashTransfer);括號沒有顯示像✐, 蚠, etc..例如字符的時候我要在[現金]字段100它顯示我的信d等等...

這裏是整個工作的代碼...

if (cmd.equals(AdminCommands[1])) { 
     String playerUsername = scanner.next(); 
     int cashTransfer = scanner.nextInt(); 

     File folder = new File(playerUsername); 
     File pFile = new File(folder, playerUsername + ".txt"); 
     File bFile = new File(folder, "balance.txt"); 
     if (pFile.exists()) { 
      try { 
       Account pAcc = new Account(playerUsername, cashTransfer); 
       FileWriter bWriter = new FileWriter(bFile); 
       BufferedWriter writer; 
       writer = new BufferedWriter(bWriter); 
       writer.write(""+cashTransfer); 
       pAcc.SaveInfo(); 
       writer.close(); 
       LabelInfo.setText("Money transfer complited ! ($" + cashTransfer + ") to (" + pAcc.username + ")"); 
      } catch (IOException e) { 
       JOptionPane.showMessageDialog(null, "ERROR: Can't save balance !"); 
      } 
     } else { 
      LabelInfo.setText("Player not found !"); 
     } 
    } 

順便說一句謝謝@Salah幫助我! :)

0

如果你的意思直接匯款到結果寫入到文件中,你可以做這樣的:

BufferedWriter writer = null; 
    try { 
     writer = new BufferedWriter(new FileWriter(pFile)); 
     writer.write(player); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      writer.close(); 
     } catch (Exception e) { 
     } 
    } 
+0

但是,如果您能夠以相同的方式理解我:/ – user3390702

+0

,那麼如何將金額轉入文件中,使用money文件初始化'BufferWriter',然後將錢串寫入它。編輯代碼 – Salah

+0

查看 – user3390702