Ask the user for a starting point and ending point. Sort the user list using the method on the previous question but only display the users within the entered range. Here is the data text. I cant copy and paste it because it is too long. https://textuploader.com/t1ol7
Ask the user for a starting point and ending point. Sort the user list using the method on the previous question but only display the users within the entered range. Here is the data text. I cant copy and paste it because it is too long. https://textuploader.com/t1ol7
I already Have a code but it doesn't sort right. I believe there is something wrong. The pic without red should be the correct answer.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ggk
{
public static void main(String[] args){
String filename = "user-database.txt";
Scanner infile = null;
try {
infile = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
ArrayList<String> usernames = new ArrayList<String>();
ArrayList<String> passwords = new ArrayList<String>();
int start, end;
Scanner input = new Scanner(System.in);
System.out.println("Enter a starting point and ending point");
start = input.nextInt();
end = input.nextInt();
infile.nextLine();
while(infile.hasNext()){
usernames.add(infile.next());
passwords.add(infile.next());
}
infile.close();
int minIdx;
String temp;
for(int i = 0; i < passwords.size();i++){
minIdx = i;
for(int j = i+1; j < passwords.size(); j++){
if(passwords.get(j).compareToIgnoreCase(passwords.get(minIdx)) < 0)
minIdx = j;
}
temp = passwords.get(i);
passwords.set(i, passwords.get(minIdx));
passwords.set(minIdx, temp);
temp = usernames.get(i);
usernames.set(i, usernames.get(minIdx));
usernames.set(minIdx, temp);
}
for(int i = start; i < usernames.size() && i < end; i++)
System.out.printf("%20s %20s\n", passwords.get(i), usernames.get(i));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps