阅读索引文件的问题

时间:2014-09-04 07:38:04

标签: java

这是文件读取问题索引1 id的arraylist存在" 22222"但是这个条件if(get.equals(low.pid))没有读取1索引处的id它会读取索引2的值。

如果我打印witharraylis.get(i) when value of i=1 it show the value of index 2. When I print witharraylist.get(i)`显示id 30的值,而在索引1中id为20的值。任何人都可以告诉我代码中的问题是什么?     //登录文件

       import java.util.*;
    import java.io.*;
     class Login{
    private  String name;
    private String id;
    private String loginname;
    private String loginid;
    static String setid;
    int j=0;
    int k=0;
    ArrayList a1=new ArrayList();
    public void setLoginId(String st1)
    {
    setid=st1;
    }
     public void loginNameId()
     {

     String adminname="muhammad ali";
     String adminpassword="789123";
     Scanner reader=new Scanner(System.in);
     System.out.println("Enter your login name");
     loginname=reader.nextLine();
     System.out.println("Enter your pin code");
     loginid=reader.nextLine();
     if(loginname.equals(adminname)&&loginid.equals(adminpassword))
     {

     System.out.println("password match");
     k++;
     }
     else
     {
      String token[]=null;  
     try{
     FileReader fr=new FileReader("login.txt");
     BufferedReader br=new BufferedReader(fr);
     String fileread=br.readLine();

     while(fileread!=null)
     {
     token=fileread.split(",");
     name=token[0];
     id=token[1];
     fileread=br.readLine();
     LoginInfo obj1=new LoginInfo(name,id);
     a1.add(obj1);

     }
     br.close();
     fr.close();
     }


     catch(IOException ex)
     {
     System.out.println(ex);

     }
     }
     }
     public void search()
     {
     for(int i=0;i<a1.size();i++)
     {
     LoginInfo p;
     p=(LoginInfo)a1.get(i);
     if(loginname.equals(p.cusname)&& loginid.equals(p.cusid))
     { 
    String s=loginid;
    setLoginId(s);
    Customer call=new Customer();
    call.displayMenu();
     j++;
     }

     }
     if(j==0&&k==0)
     {
     System.out.println("InCorrect password");

     }
     }


     }
     Customer file
    import java.util.*;
    import java.io.*;
    public class Customer{
      static String cavailablebal;
      Scanner reader=new Scanner(System.in);
      ArrayList witharraylist=new ArrayList();
      public void displayMenu()
      {
        System.out.println ("1----Withdraw Cash");
        System.out.println ("2----Cash Transfer");
        System.out.println ("3----Deposit Cash");
        System.out.println ("4----Display Balance");
        System.out.println ("5----Exit ");
        System.out.println("Enter your choice");
        char choice1=reader.next().charAt(0);
        switch(choice1)
        {
          case'1':
          withDrawal();
          break;
        }
      }//end displayMenu

      public void withDrawal()
      {

        String id,accountid,name,type,pamount,status;
        String actualbal;

        char choice1,choice2,confirm;
        int withdrawalbal,availablebal;
        int cactualbal;
        Login obj2=new Login();
        try
        {

          FileReader fr=new FileReader("account.txt");
          BufferedReader br=new BufferedReader(fr);
          String fileread=br.readLine();
          String tokens[];
          while (fileread!=null)
          {
            tokens=fileread.split(",");
            if(tokens.length>=6)
            {
              id=tokens[0];
              accountid=tokens[1];
              name=tokens[2];
              type=tokens[3];
              pamount=tokens[4];
              status=tokens[5];
              fileread=br.readLine();
              LoginInfo lo=new LoginInfo(id,accountid,name,type,pamount,status);
              witharraylist.add(lo);
            }
            else
            {
              System.out.println("wrong format");

            }
            fileread=br.readLine();
          }


          System.out.println("1----Fast Cash");
          System.out.println("2----Normal Cash");
          System.out.println("3----Exit");
          System.out.println("Enter your Choice:"); 
          choice1 = reader.next().charAt(0);
          switch(choice1)
          {
            case'1':
            System.out.println("1----500"); 
            System.out.println("2----1000"); 
            System.out.println("3----2000"); 
            System.out.println("4----5000"); 
            System.out.println("5----10000"); 
            System.out.println("6----15000"); 
            System.out.println("7----20000 ");

            System.out.println("Select one of the denominations of money:"); 
            choice2 = reader.next().charAt(0);
            if(choice2=='1')
            {
              System.out.println("Are you sure you want to withdraw Rs.500 (Y/N)?");
              confirm = reader.next().charAt(0);
              if (confirm=='y'||confirm=='Y')
              {

                String get=obj2.setid;
                for(int i=0;i<witharraylist.size();i++)
                {
                  LoginInfo low=(LoginInfo)witharraylist.get(i);
                  if(get.equals(low.pid))                                                           
                  {

                    withdrawalbal=500;
                    actualbal=low.amount;
                    cactualbal=Integer.parseInt(actualbal);
                    availablebal=cactualbal-withdrawalbal;
                    cavailablebal=Integer.toString(availablebal);
                    System.out.println("Your Available balance:"+cavailablebal);
                    witharraylist.set(i,low);
                    writeValues();
                  }
                }

              }
              else
              {

                System.out.println("Transfer not successfully");
                withDrawal();
              }
            }
            break;
          }


          br.close();
          fr.close();
        }
        catch(IOException ex)
        {
          System.out.println(ex);

        }

      }
      public void writeValues()
      {
        try{
          String line;
          FileWriter fw=new FileWriter("account.txt");
          PrintWriter pw=new PrintWriter(fw);
          for(int i=0;i<witharraylist.size();i++){
            LoginInfo wr=(LoginInfo)witharraylist.get(i);

            line=wr.pid+","+wr.accountid+","+wr.name+","+wr.type+","+wr.amount+","+wr.status;
            pw.println(line);

          }

          pw.flush();
          fw.close();
          pw.close();
        }catch(IOException ex)
        {
          System.out.println(ex);
        }
      }
    }    
//login info file


import java.util.*;
import javax.swing.*;
public class LoginInfo
{
String cusname;
String cusid;
String name;
String pid;
String accountid;
String status;
String type;
String amount;

public LoginInfo(String name,String id)
{
this.cusname=name;
this.cusid=id;
}
public LoginInfo(String id,String accountid,String name,String type,String amount,String status)

{
this.pid=id;
this.accountid=accountid;
this.name=name;
this.type=type;
this.amount=amount;
this.status=status;

}

}
     // login.txt
zeshaan,11111
nadeem,22222
ali waqar,33333
waseem,44444
jawad,55555
    //Account.txt file 
11111,10,zeshaan,current,3500,active
22222,20,nadeem,saving,44500,active
33333,30,ali waqar,saving,44500,active
44444,40,waseem,saving,44500,active
55555,50,waseem,saving,44500,active
66666,60,waseem,saving,44500,active
77777,70,waseem,saving,44500,active

1 个答案:

答案 0 :(得分:0)

我一次又一次地审核您的代码。您的程序可能将两行.txt文件视为一个并跳过第二个loginInfo,因为您只检查标记[]的前6个索引。只有可能的解释是您的account.text文件格式不符合您的说明。也许一条线被错误地格式化或其他东西。请检查.txt文件。