Java 读取索引文件时出现问题
这是文件读取问题,索引1 id处的arraylist存在“22222”,但这种情况Java 读取索引文件时出现问题,java,Java,这是文件读取问题,索引1 id处的arraylist存在“22222”,但这种情况如果(get.equals(low.pid))不读取索引1处的id,则读取索引2的值 如果我在I=1时用arraylis.get(I)打印,它将显示索引2的值。当我打印witharraylist.get(I)`时,显示id 30的值,而在索引1中,id的值是20。有人能告诉我我的代码有什么问题吗? //登录文件 import java.util.*; import java.io.*;
如果(get.equals(low.pid))
不读取索引1处的id,则读取索引2的值
如果我在I=1时用arraylis.get(I)打印,它将显示索引2的值。当我打印
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
import java.util.*;
导入java.io.*;
类登录{
私有字符串名称;
私有字符串id;
私有字符串登录名;
私有字符串loginid;
静态字符串setid;
int j=0;
int k=0;
ArrayList a1=新的ArrayList();
公共void setLoginId(字符串st1)
{
setid=st1;
}
public void loginNameId()
{
字符串adminname=“穆罕默德·阿里”;
字符串adminpassword=“789123”;
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“输入您的登录名”);
loginname=reader.nextLine();
System.out.println(“输入您的pin码”);
loginid=reader.nextLine();
if(loginname.equals(adminname)和&loginid.equals(adminpassword))
{
System.out.println(“密码匹配”);
k++;
}
其他的
{
字符串标记[]=null;
试一试{
FileReader fr=新的FileReader(“login.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串fileread=br.readLine();
while(fileread!=null)
{
token=fileread.split(“,”);
名称=令牌[0];
id=令牌[1];
fileread=br.readLine();
LoginInfo obj1=新的LoginInfo(名称、id);
a1.添加(obj1);
}
br.close();
fr.close();
}
捕获(IOEX异常)
{
系统输出打印项次(ex);
}
}
}
公开无效搜索()
{
对于(int i=0;i=6)
{
id=令牌[0];
accountid=令牌[1];
名称=代币[2];
类型=令牌[3];
pamount=代币[4];
状态=代币[5];
fileread=br.readLine();
LoginInfo lo=新的LoginInfo(id、accountid、名称、类型、pamount、状态);
witharraylist.add(lo);
}
其他的
{
System.out.println(“格式错误”);
}
fileread=br.readLine();
}
System.out.println(“1--快速现金”);
System.out.println(“2--普通现金”);
System.out.println(“3----退出”);
System.out.println(“输入您的选择:”);
choice1=reader.next().charAt(0);
开关(选项1)
{
案例'1':
System.out.println(“1---500”);
System.out.println(“2---1000”);
系统输出打印(“3---2000”);
系统输出打印项次(“4---5000”);
系统输出打印项次(“5---10000”);
系统输出打印项次(“6---15000”);
System.out.println(“7---20000”);
System.out.println(“选择一种货币:”);
choice2=reader.next().charAt(0);
如果(选项2='1')
{
System.out.println(“您确定要提取500卢比吗?”;
确认=reader.next().charAt(0);
如果(确认=='y'|确认=='y')
{
字符串get=obj2.setid;
对于(int i=0;i我已经一次又一次地检查了您的代码。您的程序可能会将两行.txt文件视为一行,并跳过第二个登录信息,因为您只检查令牌的前6个索引[]。唯一可能的解释是您的account.text文件的格式与您所述的格式不符。可能是一行格式错误或其他原因。请检查.txt文件。请格式化您的代码和您的问题。两者都很难阅读。将类型添加到ArrayList如何?除非您使用MS Word进行编码,否则应将其突出显示为w不管怎样,请给我们看一下你的account.txt文件(如果太长,也可以是其中的一部分)你能告诉我如何显示我的所有文件吗?我是StackOverflow中的新用户只需编辑你的问题,复制并粘贴文件。如果它完全如你在代码块末尾所述,我真的找不到错误。可怕的是,我希望你扣除错误,然后请检查它