[英]How a read a txt file in a array and then print in console some value in Java?
I have a problem. 我有个问题。 I have a txt file with some value, and I try to use eclipse to read and parse that txt file in a array and then put some condition to write that array in console.
我有一个具有某些值的txt文件,我尝试使用eclipse读取并解析该txt文件到数组中,然后设置一些条件以在控制台中写入该数组。 The text that I use to read txt file and parse in array is this:
我用来读取txt文件并在数组中解析的文本是这样的:
public void Transaction(String filepath, String user)
{
String [] [] myArray = new String[100][3];
Scanner scanIn = null;
int Rowc = 0, Colc = 0;
String InputLine = "";
double xnum = 0;
String username, tranzactie, info;
try
{
scanIn = new Scanner ( new BufferedReader(new FileReader(filepath)));
while(scanIn.hasNextLine())
{
InputLine = scanIn.nextLine();
String[] InArray = InputLine.split(",");
for(int x = 0; x<InArray.length; x++)
{
myArray[Rowc][x] = InArray[x];
}
Rowc++;
}
for (int i = 0; i<Rowc; i++)
{
for (Colc = 0; Colc < 3; Colc ++)
{
System.out.print(myArray[i][Colc]+ ",");
}
System.out.println();
}
}catch (Exception e)
{
System.out.println("Error");
}
}
The txt file is this: txt文件是这样的:
John,SoldInterogation
John,PayInvoice,He pay 30 EUR
Alex,PayInvoice,He pay 15 EUR
Alex,BankTransfer,He transfered 50 EUR
John,SoldInterogation
How can I write in the console just the transaction for john or Alex, or ... . 我该如何在控制台中仅编写john或Alex或...的事务。 What I must adding in my java code for do this?
为此,我必须在Java代码中添加什么? I must write only the transaction, this mean only the 2 column of the txt file, the user ( John, Alex) this musn't write in the console.
我必须只写事务,这意味着txt文件只有2列,用户(John,Alex)不会在控制台中写。
Just test if InArray is length 3, and if it is : add its two last strings to a String variable. 只需测试InArray的长度是否为3,是否为:将其最后两个字符串添加到String变量中。 Then just println this String variable after your reading loops.
然后,在您的读取循环之后,只需将此字符串变量println即可。
try
{
// A String variable to println after the reading.
String consoleResume = "";
scanIn = new Scanner ( new BufferedReader(new FileReader(filepath)));
while(scanIn.hasNextLine())
{
InputLine = scanIn.nextLine();
String[] InArray = InputLine.split(",");
for(int x = 0; x<InArray.length; x++)
{
myArray[Rowc][x] = InArray[x];
}
// There is a transaction when you have 3 strings in InArray
if (InArray.length()==3)
consoleResume+=InArray[1]+":"+InArray[2]+"\n";
Rowc++;
}
System.out.println(consoleResume);
}catch (Exception e)
{
System.out.println("Error");
}
You can also println the transactions inside your loop : 您还可以在循环内打印事务:
try
{
scanIn = new Scanner ( new BufferedReader(new FileReader(filepath)));
while(scanIn.hasNextLine())
{
InputLine = scanIn.nextLine();
String[] InArray = InputLine.split(",");
for(int x = 0; x<InArray.length; x++)
{
myArray[Rowc][x] = InArray[x];
}
// There is a transaction when you have 3 strings in inArray
if (InArray.length()==3)
System.out.println(InArray[1]+":"+InArray[2]);
Rowc++;
}
}catch (Exception e)
{
System.out.println("Error");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.