使用 toString 方法显示数组列表中的信息,无需括号和逗号

发布于 2024-11-13 08:14:05 字数 1381 浏览 3 评论 0原文

我正在使用 toString 方法来显示数组列表中的数据。如何编写代码,使其显示信息时不带 [ , , ] 围绕我的信息

请参阅下面的代码:

case 6:
            System.out.println("Enter an account number to view the transactions of the account");
            number=keyboard.nextLong();
            found=false;
            try{
                for(int i=0;i<aBank.getAccounts().size();i++){

                if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
                    found=true;
                    System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));
                    System.out.println(aBank.getAccounts().get(i).getTransaction().toString());
                    System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));
                }
                }
            }

        catch (Exception e){
            System.out.println("Unable to process request.\n" +e.getMessage());
        }
            break;

这是输出:

Account 1:  Start Balance: $1000.00
[Deposit    1   6/6/2011    $500.00
, Deposit   2   6/6/2011    $489.00
, Deposit   3   6/6/2011    $262.00
, Withdrawal    4   6/6/2011    $897.00
, Withdrawal    5   6/6/2011    $56.32
, Withdrawal    6   6/6/2011    $78.24
]
Ending Balance :$1219.44

注意需要删除的括号和逗号

I am using a toString method to display data in an arraylist. How do I write the code so that it displays the information without the [ , , ] around my information

see the code below:

case 6:
            System.out.println("Enter an account number to view the transactions of the account");
            number=keyboard.nextLong();
            found=false;
            try{
                for(int i=0;i<aBank.getAccounts().size();i++){

                if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
                    found=true;
                    System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));
                    System.out.println(aBank.getAccounts().get(i).getTransaction().toString());
                    System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));
                }
                }
            }

        catch (Exception e){
            System.out.println("Unable to process request.\n" +e.getMessage());
        }
            break;

this is the output:

Account 1:  Start Balance: $1000.00
[Deposit    1   6/6/2011    $500.00
, Deposit   2   6/6/2011    $489.00
, Deposit   3   6/6/2011    $262.00
, Withdrawal    4   6/6/2011    $897.00
, Withdrawal    5   6/6/2011    $56.32
, Withdrawal    6   6/6/2011    $78.24
]
Ending Balance :$1219.44

notice the brackets and commas that need to be removed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉薄对峙 2024-11-20 08:14:06

这又如何呢?

List<String> list = ArrayList<String>();
String asString = list.toString().replaceAll("^\\[", "").replaceAll("\\]$", "").replace(",", "");

What about this?

List<String> list = ArrayList<String>();
String asString = list.toString().replaceAll("^\\[", "").replaceAll("\\]$", "").replace(",", "");
暮年慕年 2024-11-20 08:14:06

尝试写得更具可读性。

   try {
    for(BankAccount ba : aBank.getAccounts()) {
      if(ba.getAccountNumber().equals(number)) {
        found=true;
        System.out.println("Account " + number + ":\tStart Balance: " 
         +money.format(ba.getStartBalance()));                  
        for (Object tr : ba.getTransaction()) {
          System.out.println(tr);
        }
        System.out.println("Ending Balance :" +money.format(ba.getBalance()));
       }
      }
}

Try to write it more readable.

   try {
    for(BankAccount ba : aBank.getAccounts()) {
      if(ba.getAccountNumber().equals(number)) {
        found=true;
        System.out.println("Account " + number + ":\tStart Balance: " 
         +money.format(ba.getStartBalance()));                  
        for (Object tr : ba.getTransaction()) {
          System.out.println(tr);
        }
        System.out.println("Ending Balance :" +money.format(ba.getBalance()));
       }
      }
}
謌踐踏愛綪 2024-11-20 08:14:05

根本不要使用 toString()。它不适合用于创建格式化的 UI 字符串。它是一个开发人员工具,而不是一个用户演示工具。

对于对象表示具有简单可接受格式的东西的情况,我将添加一个警告,该格式可以满足这两个目的。 Java 原始包装器(如 Integer 和 Float)就是一个例子。其他简单的类(例如 2D 点)也可以在这方面发挥作用,但随着对象变得更加复杂,这种方法很快就会崩溃。

要执行您正在尝试的操作,只需创建一个方法来进行显示并在该方法中进行格式化即可。

Don't use toString() at all. It is not meant to be used to create formatted UI strings. It is a developer tool, not a user presentation tool.

I will add a caveat for the cases where the object represents something with a simple accepted format that can serve both purposes. Java primitive wrappers (like Integer and Float) are an example of this. Other simple classes such as a 2D point can work in this regard as well, but this quickly falls apart as an object becomes more complex.

To do what you are attempting, just create a method to do your display and do the formatting in that method instead.

感受沵的脚步 2024-11-20 08:14:05

使用 番石榴加入者

Joiner.on("").join(
    aBank.getAccounts().get(i).getTransaction())

Use a guava joiner as in

Joiner.on("").join(
    aBank.getAccounts().get(i).getTransaction())
瑶笙 2024-11-20 08:14:05

您真的需要使用 toString() 方法吗?
否则,您可以自己为每个数组构建字符串。

tmpArray = aBank.getAccounts().get(i).getTransaction();
StringBuilder builder = new StringBuilder();
for (String value : tmpArray) {
  builder.append(value);
}    
System.out.println(builder.toString());

Do you really need to use the toString() Method?
Otherwise you could build up the string for each array by yourself.

tmpArray = aBank.getAccounts().get(i).getTransaction();
StringBuilder builder = new StringBuilder();
for (String value : tmpArray) {
  builder.append(value);
}    
System.out.println(builder.toString());
自由如风 2024-11-20 08:14:05

修改“getTransaction()”返回的对象的“toString”方法!?

Modify the "toString" method of the Object returned by "getTransaction()" !?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文