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. The text that I use to read txt file and parse in array is this:
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:
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 ... . What I must adding in my java code for do this? 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.
Just test if InArray is length 3, and if it is : add its two last strings to a String variable. Then just println this String variable after your reading loops.
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.