Question 1: IN JAVA! Download peopleData.txt and look through it (you can also r
ID: 3576517 • Letter: Q
Question
Question 1: IN JAVA! Download peopleData.txt and look through it (you can also rename it peopleData.csv to open in excel or some spreadsheet app). Write a program that reads this file then finds and displays on the username and password, if that line has one in the format “[username] : [password]”. This should be repeated until the end of the file (without displaying the last one twice!). You cannot assume the username will always be in the same place (or that it exists for a person at all). You may assume that if there is a username, then there will be a password (but again, you should not assume where it is located in the line). You do not need to check and see if the file properly exists here.
Example 1 (generated from peopleData.txt):
Herch1955 : ahg7aa9Kei
Ardsomal : aiCoh6Chi
Felich : eiHee3ie
Therstion : AeTai9ien3ie
Wittiptacked : ha8Thufah
Hisaim : Oocaing8
Test your program using not only the example data above, but other cases as well. And revise your program until you are sure it is correct.
Here's the people date:
Explanation / Answer
Program:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class CredentialsFinder {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Reading the text file line by line in Java using BufferedReader
FileInputStream fis = null;
BufferedReader reader = null;
// Path to the text file file
fis = new FileInputStream("C:\test\peopledata.txt");
reader = new BufferedReader(new InputStreamReader(fis));
String line;
String[] values;
while ((line = reader.readLine()) != null) {
// Split each line into values
values = line.split(";");
for (int i = 0; i < values.length; i++) {
// If values[i] is contains 'username' means next index 'i+1'
// will contain the actual username
if (values[i].contains("Username")) {
System.out.print(values[i + 1] + " : ");
break;
}
}
for (int i = 0; i < values.length; i++) {
// If values[i] is contains 'Password' means next index 'i+1'
// will contain the actual password
if (values[i].contains("Password")) {
System.out.print(values[i + 1] + " ");
break;
}
}
}
reader.close();
fis.close();
}
}
-----------------------------------------------------------------------------------------------------------------------------
Sample Output:
Herch1955 : ahg7aa9Kei
Ardsomal : aiCoh6Chi
Felich : eiHee3ie
Therstion : AeTai9ien3ie
Wittiptacked : ha8Thufah
Hisaim : Oocaing8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.