程序没有返回正确的结果和编译错误

时间:2013-11-23 22:38:24

标签: java compilation output

这段代码将成为我的终点....它现在运行正常(有点)但没有得到正确的结果....当从示例文件输入7267881时它说它无效但是对于其他人来说文件 它给出了错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The import src cannot be resolved

    at Account.<init>(Account.java:1)
    at AcccountArray.main(AcccountArray.java:45)

代码:

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();
                        //System.out.println(Account[i][0]);
                        Account[i][1]=scanner.nextLine();
                        Account[i][2]=scanner.nextLine();
                        //System.out.println(Account[i][2]);
                    }
                    scanner.close();

                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();
        }

    }

}





import src.String;

public class Account 
{

public String toString(String name,String account,String balance)
{
    System.out.println("Customer name :\t\t "+name);
    System.out.println("Account Number :\t "+ account);
    System.out.println("Current Balance :\t $"+ balance);
    return null;
}

}

测试文件

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

2 个答案:

答案 0 :(得分:1)

删除import src.String;课程之前的Account

  • 如果您需要使用java.lang.String课程,则可以在没有import声明的情况下使用
  • 但是如果您已经定义了自己的String类,则在将其从默认包中删除之前,您将无法导入它。当您将课程移至另一个课程时,import com.mypackage.String语句必须位于AccountArray课程之上。

答案 1 :(得分:1)

首先,确保没有编译问题! 在运行程序之前,请更正所有编译错误。

只有这样才能找到程序的奇怪行为。