【问题标题】:Where is the error that is not allowing account #7267881 to be valid [closed]不允许帐户#7267881有效的错误在哪里[关闭]
【发布时间】:2013-11-23 23:18:45
【问题描述】:

7267881 作为帐户在我的文件中,如果您这样做,则在扫描文件后

            System.out.println(Account[2][1]);

它会打印回 7267881,但是当提示用户输入 7267881 时,它会返回它是一个无效的号码...文件中的所有其他帐号都可以正常工作,只是不是这个...为什么?

import java.io.File;
import java.util.Scanner;


public class AcccountArray {

    public static void main(String[] args) 
    {
        //Scan the file and save account details to array
        File file = new File ("customers.txt");
        System.out.println("Path : " + file.getAbsolutePath());
        try{
            Scanner scanner = new Scanner(new File("customers.txt"));
            String[][] Account = new String[Integer.valueOf(scanner.nextLine())][3];

                    for(int i=0;i<Account.length;i++)
                    {
                        Account[i][0]=scanner.nextLine();
                        Account[i][1]=scanner.nextLine();
                        Account[i][2]=scanner.nextLine();
                    }
                    scanner.close();
                    System.out.println(Account[2][1]);
                Scanner userinput = new Scanner(System.in);
                System.out.println("Please enter account number: ");
                String accountNumber = userinput.next();
                int matchindex = 0;
                Boolean match = false;


                for (int k =0;k<Account.length;k++)
                {
                    if(Account[k][1].equals(accountNumber))
                    {
                        match = true;
                        matchindex = k;
                    }
                }


                if(match)
                {
                    Account ac =  new Account();
                    ac.toString(Account[matchindex][0], Account[matchindex][1], Account[matchindex][2]);
                    System.out.println("Enter 'D' for deposite\nEnter 'W' for withdrawal\nEnter 'Q' for quit");

                    Scanner transaction = new Scanner(System.in);
                    String type = transaction.next();

                    Scanner ammount = new Scanner(System.in);
                    switch (type) {
                    case "D":
                        System.out.println("Enter the ammount : ");
                        float diposit = ammount.nextFloat();
                        float curent = Float.valueOf(Account[matchindex][2]);
                        System.out.println("New balance = "+(curent+diposit));
                        break;
                    case "W":
                        System.out.println("Enter the ammount : ");
                        float withdrawal = ammount.nextFloat();
                        float balance = Float.valueOf(Account[matchindex][2]);
                        System.out.println("New balance = "+(balance-withdrawal));
                        break;
                    case "Q":
                        System.out.println("Exit");
                        break;

                    default:
                        System.out.println("Invalid transaction");

                    }
                }
                else
                {
                    System.out.println("Invalid user account number");
                }


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}

文件看起来像

4
John Anderson
4565413
250.00
Louise Carter 
2323472
1250.45 
Paul Johnson
7267881 
942.81
Sarah Wilson  
0982377
311.26

【问题讨论】:

  • 投票结束,因为您之前曾问过同样的问题,但没有遵循您得到的建议。清理这个乱七八糟的代码,或者使用调试器。
  • 我要求澄清这个问题,我真的不明白问题出在哪里,这是我代码中的最后一个问题请帮助
  • 我运行了一个调试器,它什么也没做
  • 我知道这很糟糕,我已经编写了不到一个月的代码,但我正在努力学习并变得更好,当你开始时人们拒绝帮助你吗?
  • 您应该更改您的 Account 类以使其更实用。在您的另一篇文章中,它有一个 toString() 方法,它......丑陋。 toString() 是一种定义明确的方法,可以创建一个说明类的字符串,它不仅仅是一种保存一些System.out.println 语句的方法。您的Account 类应在其构造函数中采用三个Strings(名称、帐户、余额),然后在其toString() 方法中创建它们的表示并返回该表示。然后,任何打电话给toString() 的人都可以打印出来,如果他们愿意的话。

标签: java


【解决方案1】:

直接的错误可以说是在您的输入文件中。有问题的帐号后面似乎有一个空格。您需要使文件中的帐号完全正确,因为它将在探测期间键入,或者修剪输入的字符串。

但是,您确实需要学习如何有效地进行调试。我有一些关于如何做到这一点的建议,有一个 Java 示例,Debug Strategy

【讨论】:

  • 比海报上的其他滥用更具建设性
  • +1,但可悲的是,他现在已经知道他可以编写最荒谬的代码并且仍然可以得到帮助。他只会修复上面的怪物,并继续以同样的方式......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2012-10-02
  • 1970-01-01
相关资源
最近更新 更多