2013-11-23 69 views
-2

这段代码将成为我的终点....它运行良好现在(有点)但没有得到正确的结果....当从示例文件中输入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

如果您不告诉我们正确的结果应该是什么样子,很难帮助您。 – nhgrif

+1

使用与类名* *相同的标识符,对于查看代码的外部人可能会有点误导。 'Account'? – ChiefTwoPencils

+2

@BobbyDigital权利,顺便说一句,帐户类是完全没用的。虽然看起来不是String [] [],他应该使用Account []。 – Ingo

回答

1

Account类之前删除import src.String;

  • 如果您需要使用java.lang.String类,你可以使用它没有一个import声明
  • 但是,如果你自己定义了String类,直到你将其删除,您将无法导入默认包。当您将课程移至另一个课程时,import com.mypackage.String声明必须位于AccountArray课程之上。
+0

不能解决问题 – user2954611

+0

@ user2954611:你的意思是如果你摆脱了这种错误的导入,你仍然有** **相同​​的错误信息? –

+0

哦,我说谎解决了其中一个,但没有检测到一个号码的错误 – user2954611

1

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

只有这样才有意义寻找程序的奇怪行为。

相关问题