In Java. Ask the user for a starting point and ending point. Sort the user list using the method in the previous question, but only display the users within the entered range. I already have a code, but it doesn't sort right. I believe there is something wrong. Here is the data text. I can't copy and paste it because it is too long. https://textuploader.com/t1ol7 The image without the red highlight should be the correct output. Code: import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class Users { 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 usernames = new ArrayList(); ArrayList passwords = new ArrayList(); 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();
In Java. Ask the user for a starting point and ending point. Sort the user list using the method in the previous question, but only display the users within the entered range. I already have a code, but it doesn't sort right. I believe there is something wrong.
Here is the data text. I can't copy and paste it because it is too long. https://textuploader.com/t1ol7
The image without the red highlight should be the correct output.
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Users
{
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 usernames = new ArrayList();
ArrayList passwords = new ArrayList();
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