CAN AN EXPERT HELP FIX MY CODE here is my code import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class User implements Comparable { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } @Override public int compareTo(User other) { int cmp = this.password.length() - other.password.length(); if (cmp != 0) { return cmp; } cmp = this.password.toLowerCase().compareTo(other.password.toLowerCase()); if (cmp != 0) { return cmp; } return this.username.toLowerCase().compareTo(other.username.toLowerCase()); } @Override public String toString() { return String.format("%20s%20s", password, username); } public static void main(String args[]){ List l= new ArrayList<>(); try{ BufferedReader br= new BufferedReader(new FileReader(new File("user-database.txt"))); String st, username, password; while((st=br.readLine())!=null){ username= st.split("\t")[0]; password= st.split("\t")[1]; l.add(new User(username,password)); } br.close(); BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); Collections.sort(l); System.out.println("Enter a starting point and ending point"); String inp[]= in.readLine().trim().split(" "); int start= Integer.parseInt(inp[0]); int end= Integer.parseInt(inp[1]); for(int i=start;i l.size()) { end = l.size(); } for (int i = start; i < end; i++) { User u = l.get(i); System.out.println(u); } but those solutions do not work, can another solution be provided to help fix my code, can an expert help fix my code so that it starts on the second line by removing the first line in bold and adding the last line in bold to display correctly, I am just one line off
CAN AN EXPERT HELP FIX MY CODE
here is my code
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class User implements Comparable<User> { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } @Override public int compareTo(User other) { int cmp = this.password.length() - other.password.length(); if (cmp != 0) { return cmp; } cmp = this.password.toLowerCase().compareTo(other.password.toLowerCase()); if (cmp != 0) { return cmp; } return this.username.toLowerCase().compareTo(other.username.toLowerCase()); } @Override public String toString() { return String.format("%20s%20s", password, username); } public static void main(String args[]){ List<User> l= new ArrayList<>(); try{ BufferedReader br= new BufferedReader(new FileReader(new File("user-database.txt"))); String st, username, password; while((st=br.readLine())!=null){ username= st.split("\t")[0]; password= st.split("\t")[1]; l.add(new User(username,password)); } br.close(); BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); Collections.sort(l); System.out.println("Enter a starting point and ending point"); String inp[]= in.readLine().trim().split(" "); int start= Integer.parseInt(inp[0]); int end= Integer.parseInt(inp[1]); for(int i=start;i<end;i++){ User u= l.get(i); System.out.println(u); } } catch(IOException e){ System.out.println(e.getMessage()); } } }
it should display
Enter a starting point and ending point\n
493 562ENTER
fpCqTjEfk cjacmardpr\n
gAmfepkQX cbidgodc4\n
GheSmEhte pianillipm\n
GMHoz13Ko lmcamishdl\n
Gr3yzaUYQ sheeranc8\n
Gv2zAbRx6 tcraddy8c\n
h7FUxSdUz rrosingb\n
HDLFE0tEN rfryd1l\n
HIlCcbD87 htongo8\n
hXB6xBgNW bfogdeneg\n
HzqustkgH mhansardpv\n
I44S3hCp9 ydranfield80\n
IE749MAQu dhouseman9a\n....
TJyeWJWUv bneamesmm\n
TLONniHHE aaltyok\n
tpXu8iQGW bwillimotcu\n
tQ8JiPSD5 ecurrorbu\n //adding this line to replace the first line but at the bottom
however. it is off one line and producing an extra line in the beginning and lacks a line at the end and instead displays:
Enter a starting point and ending point\n
493 562ENTER
fMTEc9nPe smcconway7w\n //please remove this line so it starts on the next one
fpCqTjEfk cjacmardpr\n
gAmfepkQX cbidgodc4\n
GheSmEhte pianillipm\n
GMHoz13Ko lmcamishdl\n
Gr3yzaUYQ sheeranc8\n
Gv2zAbRx6 tcraddy8c\n
h7FUxSdUz rrosingb\n
HDLFE0tEN rfryd1l\n
HIlCcbD87 htongo8\n
hXB6xBgNW bfogdeneg\n
HzqustkgH mhansardpv\n
I44S3hCp9 ydranfield80\n
IE749MAQu dhouseman9a\n
IWA54iHTY ddongallp2\n...
TJyeWJWUv bneamesmm\n
TLONniHHE aaltyok\n
tpXu8iQGW bwillimotcu\n
i have tried changing
for(int i=start;i<end;i++){
User u= l.get(i);
System.out.println(u);
}
to
for(int i=start;i<=end;i++){
User u= l.get(i);
System.out.println(u);
}
and
for(int i=start-1;i<end;i++){
User u= l.get(i);
System.out.println(u);
}
and i have tried changing loop iterations
if (end > l.size()) {
end = l.size();
}
for (int i = start; i < end; i++) {
User u = l.get(i);
System.out.println(u);
}
but those solutions do not work, can another solution be provided to help fix my code, can an expert help fix my code so that it starts on the second line by removing the first line in bold and adding the last line in bold to display correctly, I am just one line off
Trending now
This is a popular solution!
Step by step
Solved in 3 steps